Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Digital Signal Processing: 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:

2. Signal Download, Playback and Analysis


There are many ways to create a digital audio signal, including recording from a microphone,
reading from a file, and generating sound from a formula. In this section, we’ll download a
recording of an acoustical instrument, play the recording, and analyze the recording in the time
and frequency domains. Please download the recording ‘violin-C4.wav’ of a violin playing ‘C’
in the fourth octave (‘C4’) on the Western scale. Please place the file in your MATLAB directory
or in a directory on your MATLAB path. The principal frequency of ‘C4’ is 261.63 Hz.

3. Time Domain Analysis


3.1.The time-domain plot of the recording:

3.2.The average value of the signal: -0.0016

3.3.a.Time-domain plot to 1.5s to 1.6s:


3.3.b.Waves are way dense and frequency is high.
3.4.The sampling rate: fs=11025 Hz

4. Estimating the Fundamental Period


4.1.Frequency-domain plot of the recording:
4.2.Gain: 598.2121
Frequency: 261.45 Hz
Phase: -0.1616 + 0.0793i

% [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.

4.3.Spectrogram of the recording:

4.4.Range of time is the principal frequency present: 0.4s-3.1s

5. Synthesizing the Sound


5.1.a.This code is the best I can write.
fy = abs(fftshift(fourierSeriesCoeffs));
[p]=maxk(findpeaks(fy),4);
sound(p,fs);

5.1.b.Honestly there are no differences between 2 peaks and 4 peaks.


5.2.The number of largest peaks is lower, then the resulting sound sounds similar. If it increases
to 20 60 100, then the sound gets louder.
max(fourierSeriesCoeffsAbs) findpeaks(fourierSeriesCoeffsAbs)
5.3.c. Frequencies or the peaks: Nkeep = 8

% 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 = 8;
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 usinginverse FFT
synthSound = ifft(synthSoundCoeffs);
soundsc(synthSound , fs);
plot(ff,synthSound);
clear,clc;
%% exr.3
% % Read the contents of the audio file
% waveFilename ='violin-C4.wav';
% [instrumentSound , fs] = audioread(waveFilename);
% % Play back the recording with automatic scaling
% soundsc(instrumentSound , fs);
% % Plot the waveform in the time domain
% N = length(instrumentSound);
% Ts = 1/fs;
% Tmax = (N-1)*Ts;
% t = 0 : Ts : Tmax;
% figure;
% plot(t, instrumentSound);
% xlabel('Time [s]');
% ylabel('Signal amplitude');

% 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);

You might also like