Professional Documents
Culture Documents
DSP Assignment 1
DSP Assignment 1
1. Introduction
This lab will explore the use of a sum of sinusoids to analyze and synthesize a recorded note
played by a musical instrument. We will model the recorded sound as:
% [Maxvalue, Maxindex]=max(abs(fftshift(fourierSeriesCoeffs)));
% Maxfreq=ff(Maxindex);
[Maxvalue, Maxindex]=max(flip(abs(fftshift(fourierSeriesCoeffs))));
Maxfreq=ff(N+1-Maxindex);
Idea here is to find the index of max value then determine the frequency. But the commented
code results in negative frequency, since negative numbers have a lover index. To fix that I
flipped the x axis value then subtract the index from the total index. By counting from the
other side the actual index will decrease by 1. So in the end I added 1.
% exr.4
% Read the contents of the audio file
waveFilename ='violin-C4.wav';
[instrumentSound , fs] = audioread(waveFilename);
% Plot the magnitude of the frequency content
% using a discrete -time version of the Fourier series
fourierSeriesCoeffs = fft(instrumentSound);
N = length(instrumentSound);
freqResolution = fs / N;
ff = (-fs/2) : freqResolution : (fs/2)-freqResolution;
fy = abs(fftshift(fourierSeriesCoeffs));
% figure;
% plot(ff,fy);
% xlabel('f');
% figure;
% [maxValue,maxIndex]=maxk(fy,2);
% a=ff(maxIndex);
% stem(a,maxValue)
% % [Maxvalue, Maxindex]=max(abs(fftshift(fourierSeriesCoeffs)));
% % Maxfreq=ff(Maxindex);
[Maxvalue, Maxindex]=max(flip(abs(fftshift(fourierSeriesCoeffs))));
Maxfreq=ff(N+1-Maxindex);
Maxphase=fourierSeriesCoeffs(Maxindex);
%
% Plot the spectrogram
% figure;
% blockSize = round(N/4);
% overlap = round (0.875 * blockSize);
% spectrogram(instrumentSound , blockSize , overlap , blockSize , fs ,'yaxis');
[p,l]=maxk(findpeaks(fy),4);
% sound(p,fs);
% exr.5
% Needs fourierSeriesCoeffs vector computed in Section 2.3 above
Nseries = length(fourierSeriesCoeffs);
fourierSeriesCoeffsAbs = abs(fourierSeriesCoeffs);
% Nkeep must be even to have an equal number of negative and positive freq.
Nkeep = 2;
synthSoundCoeffs = zeros(Nseries , 1);
% Find the Nkeep strongest positive and negative frequency components
for n = 1 : Nkeep
[ak , k] = findpeaks(fourierSeriesCoeffsAbs);
synthSoundCoeffs(k) = fourierSeriesCoeffs(k);
fourierSeriesCoeffsAbs(k) = 0;
abs(ff(k))
end
% Convert Fourier series coefficients to time domain using inverse FFT
synthSound = ifft(synthSoundCoeffs);
soundsc(synthSound , fs);
plot(ff,synthSound);