You are on page 1of 2

Christian Yenko Section 21 I worked with Elliot. ************* Part 2 Functions ************* ***peaks.

m*** ************* function [frequencyVector] = peaks(freq,amplitude) largestPeak = max(amplitude); threshold = largestPeak/2; frequencyVector = []; positionsofMaxVectors = []; for i=1:length(amplitude) currentAmp = amplitude(i); if currentAmp > threshold frequencyVector = [frequencyVector freq(i)]; end end end **************** ***spectrum.m*** **************** function [freq, amp, cosWeights,sinWeights] = spectrum(beatVector) error(nargchk(1,1,nargin)); N = length(beatVector); if mod(N,2) == 0 M = N / 2; else error('Signal length is not even.\n'); end cosWeights = zeros(M+1,1); sinWeights = cosWeights; cosWeights(1) = mean(beatVector); t = 2*pi*(0:(N-1))/N; for m = 1:((N-1)/2) cosWeights(m+1) = 2*dot(cos(m*t), beatVector)/N; sinWeights(m+1) = 2*dot(sin(m*t), beatVector)/N; end if 2*M == N cosWeights(M+1) = dot(cos(M*t), beatVector)/N; end amp = sqrt((cosWeights).^2 + (sinWeights).^2); freq = (0:fix(N/2))*8000/N; end ***************** Exercises ***************** 1. The plot of amplitudes with respect to frequency of the single note 'a' for time '1' produces a graph with only one peak at frequency=440. The peak amplitude v alue is 1 . The rest of the graph is relatively flat, with values of amplitudes being n early zero. This makes sense, because only one note played and it is the single

note 'a' with frequency 440. 2. Since we found the spectrum for the 'a' note, we have a sin graph whose range is much smaller than that of the cosin graph. The range of the sin plot is a smal l 1.5e-15, while the range of the cosin plot is 1. The graph of the cosin plot is extremely similar to the graph of the amplitude plot simply because we used cosin in our function alread y in the PureTone.m file. 3. The two plots are definitely not the same. While the first plot (1 beat per sec ond) has one sharp peak that increases and decreases extremely quickly, the seco nd plot (4 beats per second) has a gradual peak that has a lower slope than the first plot's curve. This is due to the eve nt of leakage - whereas the first plot has no leakage because the frequency is e xactly the same frequency in the pure tones, the second plot does have leakage because it is close, but not exactly the same frequency as one of the pure tones. 4. Do the two scores match? Do they sound the same? How can you mathematically c ompare two vectors to each other? The two scores match exactly, as well as sound exactly the same. To compare the two string sequences in matlab, you would write strcmp(myScore,FurElise). To c ompare them mathematically, you would simply use the MusicMan function to return both sound vectors, and the n traverse the sound vector while checking if each value at any index 'i' is equ al to the value of the second sound vector at index 'i'.

You might also like