Digital Signal Processor Architecture Lab
Nitish S Prabhu 1MS10EC071 V Sem. ‘B’ Sec.
Table of contents
Genration of a sinusoidal wave ................................................................................................................. 1 Comparison between a pure sine wave and a sine wave affected with pseudo-random noise. ............ .............. 3 Filtering the generated noise signal .......................................................................................................... 4 Discrete Fourier Transform (Matlab optimized code) ...................... ............................... ................... ................... .................. .................. .................. ........... 6 Discrete Fourier Transform (Sequential approach) ................. .......................... ................... ................... .................. .................. .................. .................. ........... 7 Convolution of two signals ........................................................................................................................ 8 Plot the magnitude and phase response of a given system .................. ........................... .................. .................. .................. .................. ............... ...... 9
Generation of a sinusoidal waves A sinusoidal wave, Y = A* sin(w*t) of frequency 500Hz is sampled at 8kHz and is plotted against the t ime index. A cosine wave Y = A* cos(w*t) of frequency 500Hz is sampled at 8kHz and is plotted against the time index.
clc; clear all all; ; close all all; ; % Sine wave parameters A=5; N=64; n=0:N-1;
f=500; fs=8000; % Generation of sine wave w=2*pi*(f/fs); y1=A*sin(w*n); y2=A*cos(w*n); % Plotting the waves plot(n,y1,'r' plot(n,y1,'r',n,y2, ,n,y2,'b' 'b'); ); title('Sine title('Sine Wave'); Wave'); legend('Sine legend('Sine Wave', Wave', 'Cosine Wave'); Wave'); xlabel('Time xlabel('Time index'); index'); ylabel('Amplitude' ylabel('Amplitude'); ); % The output shows a pure sine wave of the required amplitude and % frequency.
Fig: Output from the sine wave generator
Comparison between a pure sine wave and a sine wave affected with pseudorandom noise. A sine wave of frequency 500Hz 500 Hz is sampled at 8kHz and is plotted against the time index. A noise component generated by the rand function is generated and added to the sine wave. The two waveforms are plotted on the same set of axes.
clc; clear all all; ; close all all; ; A=1200; N=64; n=0:N-1; f=500; fs=8000; w=2*pi*(f/fs); y=A*sin(w*n); x1=randn(size(y)).*400; x2=randn(size(y)).*400; y1=round(x1+x2+y); % Create a .dat file to store the output sine wave matrix generated. % This .dat file can be accessed by other programs to directly obtain the % data correspondind to the noisy sine wave. fid=fopen('sineNoise.dat' fid=fopen('sineNoise.dat', ,'w' 'w'); ); fprintf(fid,'%4.0f\n' fprintf(fid,'%4.0f\n',y1); ,y1); % Plot the output curve. plot(n,y,n,y1); title('Sine title('Sine Wave & Sine Wave with noise'); noise'); xlabel('Time xlabel('Time index'); index'); ylabel('Amplitude' ylabel('Amplitude'); ); legend('Sine' legend('Sine', , 'Sine + Noise'); Noise');
Filtering the generated noise signal A simple running average filter is used to filter the noise signal generated.
clc clear all close all % Length of the filter l=2; % Load the generated noise signal from the .dat file load -ascii sineNoise.dat N=size(sineNoise); n=1:N; plot(n,sineNoise,'--b' plot(n,sineNoise, '--b'); ); % Plot the filtered signal with different values of filter length for l= 1:3:16
Perform the following mathematical operation at each iter ation:
b=ones(1,(l))/(l); a=1; % Filter the signal z= filter(b,a,sineNoise); plot( n,z,'-g' n,z,'-g'); ); title('Filtered title('Filtered output'); output'); % Filtered output with the length of the filter = l; xlabel('Time xlabel('Time index'); index'); ylabel('Amplitude' ylabel('Amplitude'); );
The above figures show the variation of output with the the length of the filter. With the increase in filter length the signal is smoother but the shape of the signal is lost. There is always a t radeoff between the filter length and the accuracy in the shape of the filter.
end
Discrete Fourier Transform (Matlab optimized code) A simple implementation of discrete fourier transform algorithm and its ver ification
clear all close all clc x = [ 1 3 5 6]; N = length(x); w = exp(-1i*2*pi/N); n = 0: N-1; k=n; % Compute the twiddle factor matrix nk= n'*k; w = w.^nk;
% Compute the DFT y = x* w; disp(y); % Verify using the inbuilt function disp(fft(x));
15.0000
-4.0000 + 3.0000i
-3.0000 - 0.0000i
-4.0000 - 3.0000i
15.0000
-4.0000 + 3.0000i
-3.0000
-4.0000 - 3.0000i
Discrete Fourier Transform (Sequential approach) A sequential approach towards the same DFT algorithm
clear all close all clc x = [ 1 3 5 6]; % Computation of Twiddle factor e=exp(-1i*2*pi/length(x)); for k=1:length(x) y(k)=0; for n=1:length(x) y(k)=x(n)*(e^((k-1)*(n-1)))+y(k);
%as the limits arefrom 1 (n-1) and
(k-1) end end disp(y); % Verify using the inbuilt function disp(fft(x));
The output below shows that both methods of computing DFT will yield the same result. Computational speed is the only criteria that can demarcate the two functions.
15.0000
-4.0000 + 3.0000i
-3.0000 - 0.0000i
-4.0000 - 3.0000i
15.0000
-4.0000 + 3.0000i
-3.0000
-4.0000 - 3.0000i
Convolution of two signals Two discrete time signals are input t hrough the prompt and are convolved using the inbuilt function
clear all close all clc p
= Input('Enter the first sequence ');
q
= input('Enter the second sequence ');
nx = input('Enter input1 range '); ny = input('Enter input2 range '); l=(min(nx)+min(ny)):(max(nx)+max(ny)); % Perform the convolution d=conv(p,q); % Plot the output stem(l,d);
Fig: Convolved output from the 2 signals
Plot the magnitude and phase response of a given system Using the transfer function of the system the magnitude and phase response are plotted.
clc; clear all all; ; close all all; ; % Define the filter coefficients b=[1 0 0 0 0 ]; a=[1 -0.1 -0.9]; figure(1); [h,w]=freqz(b,a,100); zplane(b,a); ym=abs(h); ya=angle(h); figure(2); subplot(2,1,1) plot(w/pi,ym/pi) grid subplot(2,1,2) plot(w/pi,ya/pi) grid
Fig: A Pole-zero plot of the transfer function of the system under under test.
Fig: Magnitude and phase plots of the frequency response.