docx - csserver

advertisement
The following mFile creates a wav file consisting of those frequencies in the C-Major chord.
%WritePlayWav.m
wavName = 'CMajorChord.wav';
fs = 11025;
T = 1/fs;
t = 0:T:2;
f1 = 261.626;f2 = 329.628;f3 = 391.995;
y = sin(2*pi*f1*t) + sin(2*pi*f2*t) + sin(2*pi*f3*t);
tau = 1;
y = y.*exp(-t/tau);
y = .99*(y/max(abs(y)));
wavwrite(y, fs, wavName);
wavplay(y, fs);
The following m-file reads a wav file named GuitarHighNote.wav and plots it in time and frequency using the
fft.
%ReadPlotWav.m
wavName = 'GuitarHighNote.wav';
[y fs b] = wavread(wavName);
T = 1/fs;
L = length(y);
k = 0:L-1;
t = k*T;
figure(1);clf;
subplot(2, 1, 1);
plot(t, y);
axis([0 T*L -1 1]);
xlabel('Time in seconds');
ylabel('Voltage');
title(['Time plot of ' wavName]);
%
subplot(2, 1, 2);
yFFT = fft(y);
yFFT = yFFT/(max(abs(yFFT)));
deltaF = fs/L;
f = k*deltaF;
stem(f, abs(yFFT), 'MarkerSize', 0);
axis([0 5000 0 1.2]);
xlabel('Frequency in Hz');
ylabel('Gain');
title(['Frequency plot of ' wavName]);
The following mFile reads the wav file of a passing car, separates it into an approaching and leaving wave form
and plots the frequency response for the two waves on the same axis in two colors.
%DopplerCar.m
wavName = 'CarPassing6Mono.wav';
[y fs b] = wavread(wavName);
T = 1/fs;
figure(1);clf;
L = length(y);
k = 0:L-1;
plot(k*T, y);
axis([0 L*T, -1 1]);
xlabel('time in seconds');
ylabel('amplitude');
title([wavName 'in time']);
%Find center
%Use data cursor on time plot to get center at
%
010.76 seconds
indxCtr = fix(10.76/T);
%take first 90% of first half as approach wave
ind = fix(.9*indxCtr);
yA = y(1:ind);
%Go 10% past the center to the end for leave wave
ind = fix(indxCtr + .1*indxCtr);
yL = y(ind:length(y));
%Plot approach and leaving wave
figure(2);clf;
subplot(2, 1, 1);
kA = (1:length(yA));
plot(kA*T, yA);
xlabel('time in seconds');
ylabel('amplitude');
title('Approach signal in time');
subplot(2, 1, 2);
kL = 1:length(yL);
plot(kL*T, yL);
xlabel('time in seconds');
ylabel('amplitude');
title('Leaving signal in time');
%Find FFT of approach and leave
%Make yL and yA the same length
if(length(yL) > length(yA))
yL = yL(1:length(yA)); %make both same length
else
yA = yA(1:length(yL));
end
yAfft = fft(yA);
yAfft = yAfft/(max(abs(yAfft)));
yLfft = fft(yL);
yLfft = yLfft/(max(abs(yLfft)));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(3);clf
len = length(yA);
kf = (0:len-1)*fs/len;
plot(kf, abs(yAfft));
hold on;
plot(kf, abs(yLfft), 'r');
axis([0 500 0 1]);
xlabel('frequency in Hz');
ylabel('gain');
title([wavName ', fft']);
Download