Search This Blog

Sunday, June 14, 2015

matlab: Audio recording, playback and FFT

Initialising the Audio recorder 
recObj = audiorecorder                  %Create an audiorecorder object named recObj 
                                                   %for recording audio input.
                                                   %Sample frequency (Hz) - default 8000 

Prompt user to start/stop recoding & record audio for n seconds!
recordblocking(recObj, 3);            %Record your voice for 3 seconds

Manipulate the recorded sound
%play(recObj);                            %Play back the recording...I commented it in my code!
y = getaudiodata(recObj);            %Store data in an array, y.

Do Fourier Transform
Fs = 8000;                                                    % Sample frequency (Hz) 
Nsamps = length(y);
t = (1/Fs)*(1:Nsamps)                                   %Prepare time data for plot

y_fft = abs(fft(y));                                         %Retain Magnitude
y_fft = y_fft(1:Nsamps/2);                             %Discard Half of Points
f = Fs*(0:Nsamps/2-1)/Nsamps;                     %Prepare freq data for plot

Plot Sound File in Time Domain 
plot(t, y)
xlabel('Time (s)')
title('......  in Time Domain')

Plot Sound File in Frequency Domain
plot(f, y_fft)
xlim([0 1000])
xlabel('Frequency (Hz)')
title('Frequency Response of ....')