0% found this document useful (0 votes)
45 views11 pages

Digital Signal Processing: Name: Roll No: Aim

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Digital Signal Processing

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.

Code: Main File


amp=input(​'Enter the amplitude: '​); ​%taking input of amplitude
phase=input(​'Enter the phase: '​); ​%taking input of phase
f=input(​'Enter the frequency: '​); ​%taking input of frequency
periods=input(​'Enter the number of periods of wave: '​); ​%taking input of periods
fs=input(​'Enter the sampling frequency: '​); ​%taking input of sampling frequency

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;

x_n=input(​'Enter the sequence: '​); ​%taking signal as input


new_dft=dft(x_n); ​%using own function to find DFT

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}

b. Write a program to perform linear convolution using above DFT-IDFT approach


based circular convolution.

c. Plot and Verify above sequences for (a) and (b)

Algorithm using DFT-IDFT approach


We first take the dft of both sequences and then multiply their outputs. The second step would
be to take the IDFT of the result of multiplication of both the DFTs and then dividing the IDFT
with the length of output from multiplication of both DFTs.

Code:
clc;
clear all;
close all;

x1=input(​'Enter 1st sequence: '​); ​%taking first sequence from user


x2=input(​'Enter 2nd sequence: '​); ​%taking second sequence from user
len = ​length​(x1)+​length​(x2)​-1​; ​%calculating length for final sequence

x11=[x1, zeros(​1​,length(x2)​-1​)]; ​%padding zeroes


x22=[x2, zeros(​1​,length(x1)​-1​)]; ​%padding zeroes

maximum=max(​length​(x1),​length​(x2)); ​%finding the maximum of 2 lengths


x1=[x1, zeros(​1​,maximum-length(x1))];
x2=[x2, zeros(​1​,maximum-length(x2))];

% --------- FOR CIRCULAR CONVOLUTION --------------


% DFT of sequence 1
N=​length​(x1);
for​ ii=​1​:N
output_1(ii)=​0​;
​for​ n=​1​:N
output_1(ii)=output_1(ii)+x1(n).*​exp​(​-1j​.*​2.​*​pi​.*(n​-1​).*(ii​-1​)./N);
​end
end
%DFT of sequence 2
N=​length​(x2);
for​ ii=​1​:N
output_2(ii)=​0​;
​for​ n=​1​:N
output_2(ii)=output_2(ii)+x2(n).*​exp​(​-1j​.*​2.​*​pi​.*(n​-1​).*(ii​-1​)./N);
​end
end
answer=output_1.*output_2; ​%multiplying both DFTs
N=​length​(answer);

%Finding IDFT for CIRCULAR CONVOLUTION


for​ ii=​1​:N
z3(ii)=​0​;
​for​ n=​1​:N
z3(ii)=z3(ii)+answer(n)*​exp​(​1j​*​2​*​pi​*(n​-1​)*(ii​-1​)/N);
​end
end
x=(​1​/N).*z3;
n1=​1​:maximum;
subplot(​2,1,1​);
stem(n1,x);
title(​'Circular convolution using DFT and IDFT '​);

% ------- FOR LINEAR CONVOLUTION -------------


%DFT for sequence 1
N=​length​(x11);
for​ ii=​1​:N
output_1(ii)=​0​;
​for​ n=​1​:N
output_1(ii)=output_1(ii)+x11(n).*​exp​(​-1j​.*​2.​*​pi​.*(n​-1​).*(ii​-1​)./N);
​end
end

%DFT for sequence 2


N=​length​(x22);
for​ ii=​1​:N
output_2(ii)=​0​;
​for​ n=​1​:N
output_2(ii)=output_2(ii)+x22(n).*​exp​(​-1j​.*​2.​*​pi​.*(n​-1​).*(ii​-1​)./N);
​end
end
answer=output_1.*output_2;
N=​length​(answer);

%finding IDFT for linear convolution


for​ ii=​1​:N
z3(ii)=​0​;
​for​ n=​1​:N
z3(ii)=z3(ii)+answer(n)*​exp​(​1j​*​2​*​pi​*(n​-1​)*(ii​-1​)/N);
​end
end
x=(​1​/N).*z3;
n2=​1​:len;
subplot(​2,1,2​);
stem(n2,x);
title(​'Linear convolution using DFT and IDFT '​);

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)]

● The given zero padding are for Circular convolution


x1=[x1, zeros(1,maximum-length(x1))]
x2=[x2, zeros(1,maximum-length(x2))]

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

y_filter = filter(Fs/​2​, ​1​, y); ​% normalizes the filter coefficient


plot(y);
title(​'Original Sound'​)
figure
[b,a] = butter(​3​,[​0.3​ ​0.7​],​'bandpass'​); ​%returns the transfer function coefficients

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.

You might also like