Digital Signal Processing: Name: Roll No: Aim
Digital Signal Processing: Name: Roll No: Aim
Digital Signal Processing: Name: Roll No: Aim
Lab - 4
Name : Charmil Gandhi
Roll No : 1741059
Aim: To understand the concepts of Discrete Time Fourier Transform(DTFT) and DFT and its
application.
-------------------------------------------------------------------------------------------------------------------------------
1. Consider analog sinusoidal wave x(t )= 4cos(100𝝅t) is sampled at twice the Nyquist rate
for a full one period. Find out its DFT. If x(t) is sampled at Nyquist rate for one period
what will be its DFT? Instead of one period if two full periods are sampled then how is
the result affected in both cases?
Code: dft Function
function [out] = dft(x)
N=length(x);
for k=1:N
out(k)=0;
for n=1:N
out(k)=out(k)+x(n).*exp(-1j.*2.*pi.*(n-1).*(k-1)./N);
end
end
end
The above code is a user made dft function which takes sequence x as the input. Firstly, I have
found the N which is equal to the length of sequence x. Then, a for loop iterated from k=1 to N
in which the resultant sequence out is initialised to 0. In another nested loop for n=1 to N, the
formula of dft is applied.
f_new=f/fs;
N=fs/gcd(f,fs);
f_diff=fs/(N*periods);
n=(0:(periods*N-1));
xn=amp*cos(2*pi*f_new*n + phase); %generating the cosine signal
subplot(3,1,1),
stem(n,xn);
fx=dft(xn); %using own function of dft
y=abs(fx);
n=n*f_diff;
title('Original Signal');
subplot(3,1,2),
stem(n,y);
fx=fft(xn); %using inbuilt function
title('DFT using own function');
subplot(3,1,3),
stem(n,fx);
title('DFT using inbuilt function');
figure
stem(n,y)
I am taking amplitude (amp), phase, frequency(f), sampling frequency(fs) and periods. I have
sampled the frequency so f_new = f/fs and n=(0 :(periods*N- 1)). In the next step I have
defined the signal xn. Now, I have called the user made dft function and the inbuilt MATLAB
function fft. Using subplot c ommand I have plotted more than one graph in a single figure.
2. Use MATLAB to compute the DFT of the following sequences and verify the answer by
finding the IFFT command. Display output sequences on the command window and also
obtain magnitude and phase spectrum.
Note: In this question the user made dft function is the same as the one used in question
a) x(n) = {1,2,3,4}
Code:
clc;
clear all;
close all;
subplot(3,1,1);
stem(x_n);
title('Original Sequence')
subplot(3,1,2);
stem(new_dft); %plotting the dft
title('DFT of Sequence')
subplot(3,1,3);
stem(ifft(new_dft)); %using IFFT command to verify
title('Verification of Answer')
new_dft;
ifft(new_dft);
mag = abs(new_dft); %finding the magnitude
phase_spectrum = unwrap(angle(new_dft)); %finding the phase spectrum
mag;
phase_spectrum;
Taking the signal x_n as user input. Now, I have called the user made function dft and then
called the inbuilt MATLAB function ifft to find the inverse fourier transform and verify the answer
obtained from dft function. Also to find the magnitude, I have used abs function and to
determine the phase_spectrum, I have used the unwrap function. Both of these functions have
f_dft and angle(new_dft) as the input parameter respectively.
b) x(n) = u(n) - u(n-3) for N=4 and N= 8
Code:
clc;
clear all;
close all;
n = -10:0.01:10;
u_n = (n>=0);
u_nMinus3 = ((n-3)>=0);
x_n = u_n - u_nMinus3;
N_1 = 4;
for ii=1:N_1
output(ii)=0;
for jj=1:N_1
output(ii)=output(ii)+x_n(jj+1000-1).*exp(-1j.*2.*pi.*(jj-1).*(ii-1)./N_1);
end
end
n = 1:4;
subplot(4,1,1);
plot(n,output);
title('DFT - 4 point')
subplot(4,1,2);
plot(n,ifft(output));
title('Inverse - 4 point')
N_2 = 8;
for ii=1:N_2
output(ii)=0;
for jj=1:N_2
output(ii)=output(ii)+x_n(jj+1000-1).*exp(-1j.*2.*pi.*(jj-1).*(ii-1)./N_2);
end
end
n = 1:8;
subplot(4,1,3);
plot(n,output);
title('DFT - 8 point')
subplot(4,1,4);
plot(n,ifft(output))
title('Inverse - 8 point')
Firstly I have defined the array n ranging from -10 to +10 with increment of 0.01. Then I
have defined the unit step function: u(n) and u(n-3). From These two unit step,
xn=u(n)-u(n-3). Now for N=4 and N=8, dft has been performed using nested for loops
he subplot command has been used to plot multiple graphs in the single
for ii=1 to N. T
figure. Also, ifft command was used to verify the output.
3. Convolution Application using DFT-IDFT
a. Write a MATLAB program to find circular convolution of two sequences using
DFT-IDFT based approach.
x1(n) = {1,-1,-2,3,-1} and x2(n) = {1,2,3}
x1(n) = {1, 2, 1, 2} and x2(n)= {3,2,1,4}
Code:
clc;
clear all;
close all;
First I am taking both the sequences x1 a nd x2 as user input. The next step would be to find the
length of resultant of linear convolution and circular convolution. The length of result from linear
convolution would be len = length(x1)+length(x2)-1.The length of result from circular
convolution would be the maximum of both the sequence given as
maximum=max(length(x1),length(x2)). The next step is pad zeros accordingly.
● The given zero padding are for linear convolution
x11=[x1, zeros(1,length(x2)-1)]
x22=[x2, zeros(1,length(x1)-1)]
Now we will be applying the algorithm that I have written just before the code on x11 and x22
for linear convolution and on x1 and x2 for circular convolution. Using subplot c ommand, I have
plotted multiple graphs in a single figure.
4. Perform audio processing operations like addition of noise to the audio, creating the
butterworth filter and filtering the noisy audio and storing it in a new file.
Note: I have also submitted all the audio files
Code:
clc;
clear all;
[y,Fs] = audioread('sample_sound.wav');
len = length(y);
freq = Fs;
y_shifted = fftshift(y); %Shift zero-frequency component to center of spectrum
%sound(y_shifted,Fs);
freqz(b,a)
title('Magnitude and Phase Spectrum')
figure
t = -0.5:1/192676:0.5-(1/192676);
y_with_noise = transpose(y) + sin(100*pi*t); %adding noise to sound
%sound(transpose(y_with_noise),Fs); %playing noisy sound
% plot(y_with_noise)
% title('Noisy Audio')
audiowrite('Noisy_Audio.wav',y_with_noise,Fs)
[b_new,a_new] = butter(3,[0.3 0.7],'bandpass'); %finding butterworth
coefficient
y_new = filter(b_new,a_new,y_with_noise); %filtered audio
%sound(y_new) %playing filtered sound
plot(y_new);
title('After applying butterworth filter')
audiowrite('Filtered_Audio.wav',y_new,Fs
First, by using the audioread function, I am reading the audio sample_sound.wav and storing
it in matrix y and also its sampling frequency in Fs. The output of fftshift i s stored in y_shifted.
By using filter function, I have normalized the filter coefficient. By using the butter function, I
have obtained the coefficients of the transfer function of a bandpass filter. Freqz function is
used to obtain the frequency response using the coefficients of the transfer function.
As the size of y was 192675x1, I have defined the array t r anging from -0.5 to +(0.5-1/192676). I
have done this because the noise that is to be added to the audio will be a sinusoidal wave and
to make the sample equal to that of y. N ow the noisy audio is defined as
y_with_noise=transpose(y) + sin(100*pi*t). By using the audiowrite function, I have
stored the audio file. By using butte, n ew transfer function coefficients were found and I have
used these coefficients in the filter f unction, thus obtaining a butterworth filter. I have passed
y_with_noise i n filter, thus obtaining the new filtered audio.
Conclusion:
In this lab we learnt about DFT, DTFT, inverse fourier transform and also their application in
audio filtering using butterworth filters.