-
Notifications
You must be signed in to change notification settings - Fork 1
/
fftwav.m
44 lines (37 loc) · 1.47 KB
/
fftwav.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
% wavfft.m
%
% An example Matlab script to demonstrate the use of the fft
% function with WAV-file data.
%
% Gary Scavone
% McGill University, 2004.
% The following function call assumes that the file 'triangle.wav' is
% in the current directory or a location in the Matlab path.
[y, fs, nbits] = wavread('x11.wav');
% Use the sound function to play the data at the original sample
% rate.
disp('Playing at the original sample rate.');
sound(y, fs);
% First try the specgram function on the sound.
specgram(y);
disp('Hit any key to continue ...');
pause
% OK, now calculate the signal fft and plot the magnitude
% spectrum. Note that ffts of very long signals can take a long
% time to compute. Typically, one takes ffts of smaller chunks of
% the sound signal and views their progression over time.
Y = fft(y);
plot(abs(Y));
% Notice how the spectrum magnitude repeats itself. This always
% happens when computing an fft of a real signal. As we know from the
% sampling theorem, only frequency content up to half the sample rate
% (or Nyquist rate) can be correctly distinguished in discrete-time
% signals. Therefore, we really don't need to view the second half
% of the fft data.
%
% We can use the axis function to "zoom in" on the first half of
% the data.
axis([0 length(Y)/2, 0 max(abs(Y))])
% It would be nice to plot this data vs. frequency values along the
% x-axis (either in Hz or radians). It would also be nice to have
% axis labels. But we'll leave that for the homework.