Basic Digital Signal Processing Matlab Codes
Basic Digital Signal Processing Matlab Codes
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
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',n,y2,'b'); title('Sine Wave'); legend('Sine Wave', 'Cosine Wave'); xlabel('Time index'); ylabel('Amplitude'); % The output shows a pure sine wave of the required amplitude and % frequency.
Comparison between a pure sine wave and a sine wave affected with pseudorandom noise.
A sine wave of frequency 500Hz 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; close 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','w'); fprintf(fid,'%4.0f\n',y1); % Plot the output curve. plot(n,y,n,y1); title('Sine Wave & Sine Wave with noise'); xlabel('Time index'); ylabel('Amplitude'); legend('Sine', 'Sine + Noise');
b=ones(1,(l))/(l); a=1; % Filter the signal z= filter(b,a,sineNoise); plot( n,z,'-g'); title('Filtered output'); % Filtered output with the length of the filter = l; xlabel('Time index'); ylabel('Amplitude');
The above figures show the variation of output with 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 tradeoff between the filter length and the accuracy in the shape of the filter.
end
% Compute the DFT y = x* w; disp(y); % Verify using the inbuilt function disp(fft(x));
15.0000 15.0000
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 15.0000
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: A Pole-zero plot of the transfer function of the system under test.