Thursday, October 11, 2012

MATLAB - FFT of Sound Wave

sound_fft_mg
% =====================================================================
% MATLAB code to record sound and display the wave form in both
% time domain and frequency domain
% By Mahesha MG, MIT Manipal
% Date: 11/10/2012
% =====================================================================
a1=analoginput('winsound',0);
addchannel(a1,1)
input('Press Any key to record','s')
start(a1)
y=getdata(a1);
Fs = 8000;                    % Sampling frequency
T = 1/Fs;                     % Sample time
L = 8000;                     % Length of signal
t = (0:L-1)*T;                % Time vector
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
figure(1)
plot(t,y)
title('Wave form in time domain')
% Plot single-sided amplitude spectrum.
figure(2)
stem(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
   Index:  ChannelName:  HwChannel:  InputRange:  SensorRange:  UnitsRange:  Units:   
   1       'Mono'        1           [-1 1]       [-1 1]        [-1 1]       'Volts'  





1 comment:

  1. How do I code so that I can use fft to transform a function in coordinate space f(x) into a function in momentum space f(p). Both are complex wave functions, both x and p have negative values.

    ReplyDelete