Digital Signal Processing Lab ADVANCED DIGITAL SIGNAL PROCESSING LAB Submitted to: Submitted by: Dr. Debabrata Ghosh Twinkle Bansal 801961005 ME-ECE 1|Page Digital Signal Processing Lab INDEX S. No. Name of the Experiment 1. Write a program Practical Data Submission Date Remarks a. To generate unit impulse, unit step, and Signum function. b. To generate the square waves (both continuous and discrete plot) with user input frequencies and duty cycle. c. To generate the sinusoidal signal with user input multiple frequencies, amplitude, and its phase. 2. Write a program a. To determine N-point DFT of the user input sequence (without using built in command) and plot its magnitude and phase spectrum. b. Compare the IDFT of the results obtained in (a). 3. WAP to determine the Linear Convolution (without using inbuilt command) of any two discrete sequences. 4. WAP to determine the Circular Convolution (without using inbuilt command) of user defined two discrete sequences. 5. WAP to determine the Circular Convolution of any two discrete sequences using DFT and IDFT. 6. WAP to determine the time domain and frequency domain response of fixed windows by taking N=25. 2|Page Digital Signal Processing Lab 7. WAP to design the FIR Low filter using windows by user parameters. 8. Write a program to perform the decimation without using in-built function. 3|Page Digital Signal Processing Lab Experiment No-1 AIM: Write a program a) To generate unit impulse, unit step, and Signum function. b) To generate the square waves (both continuous and discrete plot) with user input frequencies and duty cycle. c) To generate the sinusoidalsignal with user input multiple frequencies, amplitude, and its phase. SOFTWARE REQUIRED: MATLAB R2016a MATLAB CODE: Part 1 (a) %To generate unit impulse, unit step, and Signum function. clear all close all clc % Unit Impulse Function x = -10:1:10; y = [zeros(1,10) ones(1,1) zeros(1,10)]; subplot(3,1,1) stem(x,y) title('Unit Impulse Function (801961005)') xlabel('Time') ylabel('Amplitude') % Unit Step Function x1 = -10:1:10; y1 = [zeros(1,10) zeros(1,1) ones(1,10)]; subplot(3,1,2) 4|Page Digital Signal Processing Lab stem(x1,y1) title('Unit Step Function (801961005)') xlabel('Time') ylabel('Amplitude') % Signum Function x3 = -10:1:10; y3 = sign(x3); subplot(3,1,3) stem(x3,y3) title('Signum Function (801961005)') xlabel('Time') ylabel('Amplitude') OUTPUT: Part 1 (a) 5|Page Digital Signal Processing Lab MATLAB CODE: Part 1 (b) %% clear all close all clc %-------- Case1: Duty cycle=25% and frequency=5Hz --------% % Continous Plot % Time t = 1:0.01:10; duty_cycle = 25; freq = 5; x = square(2*pi*freq*t,duty_cycle); subplot(3,2,1) plot(t,x) title('Continous => Duty cycle=25% and frequency=5Hz (801961005)') xlabel('Time') ylabel('Amplitude') % Discrete Plot subplot(3,2,2) stem(t,x) title('Discrete => Duty cycle=25% and frequency=5Hz (801961005)') xlabel('Time') ylabel('Amplitude') %-------- Case2: Duty cycle=50% and frequency=10Hz --------% % Continous Plot % Time t = 1:0.01:10; duty_cycle = 50; freq = 10; 6|Page Digital Signal Processing Lab x = square(2*pi*freq*t,duty_cycle); subplot(3,2,3) plot(t,x) title('Continous => Duty cycle=50% and frequency=10Hz (801961005)') xlabel('Time') ylabel('Amplitude') % Discrete Plot subplot(3,2,4) stem(t,x) title('Discrete => Duty cycle=50% and frequency=10Hz (801961005)') xlabel('Time') ylabel('Amplitude') %-------- Case3: Duty cycle=75% and frequency=15Hz --------% % Continous Plot % Time t = 1:0.01:10; duty_cycle = 75; freq = 15; x = square(2*pi*freq*t,duty_cycle); subplot(3,2,5) plot(t,x) title('Continous => Duty cycle=75% and frequency=15Hz (801961005)') xlabel('Time') ylabel('Amplitude') % Discrete Plot subplot(3,2,6) stem(t,x) title('Discrete => Duty cycle=75% and frequency=15Hz (801961005)') xlabel('Time') ylabel('Amplitude') 7|Page Digital Signal Processing Lab OUTPUT: Part 1 (b) MATLAB CODE: Part 1 (c) %% close all clear all clc % Case 1: f=0.8,A=100,Phase=90 % Time t=0:0.001:10; % Frequency freq= 0.8; % Amplitude Amp = 100; 8|Page Digital Signal Processing Lab % Phase phase = 90; % sin wave Out = Amp*sin(2*pi*freq*t); %Plot subplot(2,1,1) plot(t,Out) grid on xlabel('Time') ylabel('Output Signal') title('Sinusoidal signal,f=0.8,A=100,Phase=90 (801961005)') % Case 2: f=5,A=50,Phase=45 % Time t=0:0.001:10; % Frequency freq= 5; % Amplitude Amp = 50; % Phase phase = 45; % sin wave Out = Amp*sin(2*pi*freq*t); %Plot subplot(2,1,2) plot(t,Out) grid on title('Sinusoidal signal,f=5,A=50,Phase=45 (801961005)') xlabel('Time') ylabel('Output Signal') 9|Page Digital Signal Processing Lab OUTPUT: Part 1 (c) 10 | P a g e Digital Signal Processing Lab Experiment No-2 AIM: Write a program a) To determine N-point DFT of the user input sequence (without using built in command) and plot its magnitude and phase spectrum. b) Compare the IDFT of the results obtained in (a). SOFTWARE REQUIRED: MATLAB R2016a MATLAB CODE: close all clear all clc x = input('Enter the sequence for FFT = '); N = length(x); %finding the length of entered sequence for N point FFT for k = 1:N y(k) = 0; for n = 1:N % FFT function y(k) = y(k)+x(n)*exp(-i*2*pi*(k-1)*(n-1)/N); end end t_value = 0:N-1; subplot(2,2,1); stem(t_value,x); grid on; ylabel('Amplitude'); xlabel('K'); title('Input Sequence (801961005)'); 11 | P a g e Digital Signal Processing Lab % Magnitudes of individual FFT points magnitude = abs(y); subplot(2,2,2); stem(t_value,magnitude); grid on; ylabel('Amplitude'); xlabel('n'); title('Magnitude (801961005)'); % Phase Plot Phase_1 = angle(y); disp(Phase_1); subplot(2,2,3); stem(t_value,Phase_1); grid on; ylabel('Phase'); xlabel('n'); title('Phase (801961005)'); % IDFT L_new = length(y); for n = 1:L_new x_new(n) = 0; for k = 1:L_new x_new(n) = x_new(n)+(1/L_new)*y(k)*exp(1i*2*pi*(k-1)*(n-1)/L_new); end end t_value = 0:L_new-1; subplot(2,2,4); stem(t_value,x_new); ylabel('Amplitude'); xlabel('n'); 12 | P a g e Digital Signal Processing Lab title('IFFT sequence (801961005)'); grid on; OUTPUT: 13 | P a g e Digital Signal Processing Lab Experiment No-3 AIM: WAP to determine the Linear Convolution (without using inbuilt command) of any two discrete sequences. SOFTWARE REQUIRED: MATLAB R2016a MATLAB CODE: %% Linear Convolution close all clear all clc % num is the range of input variables num = 15; % Random variable L=randi(10,1,num); M= randi(10,1,num); m =length(M); n =length(L); N = m+n-1; M1=[L,zeros(1,m)]; L1=[M,zeros(1,n)]; % Linear Convolution without using in-built command for i =1:N y(i)=0; for j=1:i val = (i-j+1); if(val>0) y(i)=[y(i)+L1(j)*M1(val)]; 14 | P a g e Digital Signal Processing Lab end end end % Plot subplot(2,1,1) stem(y,'linewidth',2) grid on title('Linear Convolution (801961005)') xlabel('n') ylabel('Y(n)') % Linear Convolution using in-built command Out = conv(L,M); subplot(2,1,2) stem(Out,'linewidth',2) grid on title('Linear Convolution using in-built command (801961005)') xlabel('n') ylabel('Y(n)') 15 | P a g e Digital Signal Processing Lab OUTPUT: 16 | P a g e Digital Signal Processing Lab Experiment No-4 AIM: WAP to determine the Circular Convolution (without using inbuilt command) of user defined two discrete sequences. SOFTWARE REQUIRED: MATLAB R2016a MATLAB CODE: %% Circular Convolution close all clear all clc seq = input('Enter the sequence 1'); seq1 = input('Enter the sequence 2'); % num is the range of input variables num = length(seq); num1 = length(seq1); % Random variable L = seq; M = seq1; N = max(num,num1); if num>num1 L1=[L,zeros(1,num1)]; M1=[M,zeros(1,num)]; else L1=[L,zeros(1,num)]; M1=[M,zeros(1,num1)]; end y = zeros(1,N); % Circular Convolution without using in-built command 17 | P a g e Digital Signal Processing Lab for i =0:N-1 for j=0:N-1 val=mod(i-j,N); y(i+1)=[y(i+1)+M1(j+1).*L1(val+1)]; end end % Plot subplot(2,1,1) stem(y,'linewidth',2) grid on title('Circular Convolution (801961005)') xlabel('n') ylabel('Y(n)') % Circular Convolution using in-built command Out = cconv(M,L,N); subplot(2,1,2) stem(Out,'linewidth',2) grid on title('Circular Convolution using in-built command(801961005)') xlabel('n') ylabel('Y(n)') 18 | P a g e Digital Signal Processing Lab OUTPUT: 19 | P a g e Digital Signal Processing Lab Experiment No-5 AIM: For any two-user input discrete sequence compute circular convolution using DFT-IDFT method (without inbuilt command). SOFTWARE REQUIRED: MATLAB R2016a MATLAB CODE: %% Circular Convolution close all clear all clc %circular convolution using DFT-IDFT (without inbuilt command) xn=input('enter sequence 1='); hn=input('enter sequence 2='); L1=length(xn); L2=length(hn); N=max(L1,L2); xn=[xn,zeros(1,N-L1)]; hn=[hn,zeros(1,N-L2)]; y=zeros(1,N); F=fft(xn,N); J=fft(hn,N); Q=F.*J; I=ifft(Q) subplot(2,1,1) stem(I); title('circular convolution using DFT-IDFT(without inbuilt command)’) xlabel('n') ylabel('y(n)') 20 | P a g e Digital Signal Processing Lab %Circular convolution with inbuilt command out=cconv(xn,hn,N); subplot(2,1,2) stem(out) title('circular convolution with inbuilt command(801961005)') xlabel('n') ylabel('y(n)') OUTPUT: 21 | P a g e Digital Signal Processing Lab Experiment No-6 AIM: Write a program to determine time domain and frequency domain response of fixed windows by taking N=25. SOFTWARE REQUIRED: MATLAB R2016a MATLAB CODE: %% Windows close all clear all clc % Hamming Window N = 25; for n = 0:N w(n+1) = 0.54 - 0.46*cos(2*pi*n/(N)); end subplot(421) plot(0:N,w,'linewidth',1) xlabel('Samples') ylabel('Amplitude') title('Hamming--Time Domain Plot') grid on [t,f1] = freqz(w); t1 = abs(t); subplot(422) plot(f1/pi,10*log(t1),'linewidth',1) xlabel('Normalized Frequency') ylabel('Magnitude[dB]') title('Hamming--Frequency Domain Plot') 22 | P a g e Digital Signal Processing Lab grid on clear all % Hanning Window N = 25; for n = 0:N w(n+1) = 0.5 - 0.5*cos(2*pi*n/(N)); end subplot(423) plot(0:N,w,'linewidth',1) xlabel('Samples') ylabel('Amplitude') title('Hanning--Time Domain Plot') grid on [t,f1] = freqz(w); t1 = abs(t); subplot(424) plot(f1/pi,10*log(t1),'linewidth',1) xlabel('Normalized Frequency') ylabel('Magnitude[dB]') title('Hanning--Frequency Domain Plot') grid on clear all % Rectangular Window N = 25; for n = 0:N w(n+1) = 1; end subplot(425) plot(0:N,w,'linewidth',1) xlabel('Samples') 23 | P a g e Digital Signal Processing Lab ylabel('Amplitude') title('Rectangular--Time Domain Plot') grid on [t,f1] = freqz(w); t1 = abs(t); subplot(426) plot(f1/pi,10*log(t1),'linewidth',1) xlabel('Normalized Frequency') ylabel('Magnitude[dB]') title('Rectangular--Frequency Domain Plot') grid on clear all % Blackman Window N = 25; for n = 0:N w(n+1) = 0.42 - 0.5*cos(2*pi*n/N)+ 0.08*cos(4*pi*n/N); end subplot(427) plot(0:N,w,'linewidth',1) xlabel('Samples') ylabel('Amplitude') title('Blackman--Time Domain Plot') grid on [t,f1] = freqz(w); t1 = abs(t); subplot(428) plot(f1/pi,10*log(t1),'linewidth',1) xlabel('Normalized Frequency') ylabel('Magnitude[dB]') title('Blackman--Frequency Domain Plot') 24 | P a g e Digital Signal Processing Lab OUTPUT: 25 | P a g e Digital Signal Processing Lab Experiment No-7 AIM: Design FIR low pass filter using window parameters which includes Pass band Edge frequency, Sampling frequency, Transition Width, and Stop Band Attenuation. SOFTWARE REQUIRED: MATLAB R2016a MATLAB CODE: %% close all clear all clc fp = 1500; fs = 800; tw = 500; fs_att = 19; fp1 = fp/fs; tw1 = tw/fs; fc = fp1+ tw1/2; % N = round(0.9/tw1); N = 40; n = (-(N-1)/2):(N-1)/2; rectangular = [ones(1,N)]; h = [ones(1,N)]; hd = [ones(1,N)]; for i = 1:N wc = 2*pi*fc; h(i) = 2*fc*sin(n(i)*wc)/(n(i)*wc); hd(i) = h(i)*rectangular(i); end subplot(121); stem(n,hd); title('LPF with RECTANGULAR WINDOW'); xlabel('n'); ylabel('Filter Coeff.h'); [X2,w2] = freqz(hd,1,1024); % X2 = fftshift(X2); 26 | P a g e Digital Signal Processing Lab subplot(122); plot(w2/(2*pi),20*log10(abs(X2)/max(X2))); title('LPF with RECTANGULAR WINDOW Frequency Response'); xlabel('Normalised f'); ylabel('Norm.Magnitude Resp.(dB)'); OUTPUT: 27 | P a g e Digital Signal Processing Lab Experiment No-8 AIM: Write a program to perform the decimation without using in-built function. SOFTWARE REQUIRED: MATLAB R2016a MATLAB CODE: %% close all clear all clc n = 0:50; x = sin(n); h = fir1(128-1,1/3); xf = filter(h,1,x); yf = downsample(xf,3); ynf = downsample(x,3); f = -1:1/512:1; X = freqz(x,1,2*pi*f); Xf = freqz(xf,1,2*pi*f); Yf = freqz(yf,1,2*pi*f); Ynf = freqz(ynf,1,2*pi*f); subplot(411) plot(f,abs(X)) title('Unfiltered Input') subplot(412) plot(f,abs(Ynf)) title('Down by 3 w/o Filter Output (Showing Aliasing)') subplot(413) plot(f,abs(Xf)) 28 | P a g e Digital Signal Processing Lab title('Filtered Input') subplot(414) plot(f,abs(Yf)) title('Down by 3 with Filter Output') OUTPUT: 29 | P a g e