DSP Lab Manual
DSP Lab Manual
DEPARTMENT OF ECE
DSP LAB MANUAL
emblem
Cycle -2
1. 2. 3. 4. 5. 6. Generation of Sinusoidal signal through filtering Generation of DTMF signal Implementation of Decimation process Implementation of Interpolation process Implementation of ID sampling rate converters Audio application to plot a time and frequency display of microphone plus a cosine using DSP 7. addition of noise above 3Khz and then remove, interference suppression using 400 Hz tone 8. Impulse response of 1st order and 2nd order system
AIM: To develop mat lab code for generation of different types of sinusoidal signals and to verify it using MATLAB software Apparatus Required: MATLAB Software 10
Program: % unit impulse signal tmin = -5, -dt= 0.1, tmax=5; t=tmin : dt : tmax; x1 = 1 x2 = 0 x = x1*(t>= 0) + x2*(tn=0); figure(1) subplot(3,1,1); plot(t,x); x label (t); y label (x(t)); title (unit impulse signal); y unit step signal x1 =1 x2=0
x= x1*(t>=0) + x2*(t<0) Subplot (3,1,2); Plot (t, x); X label (t); Y label (x(t)); Title (unit step signal); Subplot (3,1,3); Stem (t,x); X label (t); Y label ( x(t)); Title (unit step sequence); % unit ramp signal x1= t x2 =0 x = x1*(t>0)+x2*(t<0) Figure (2) Subplot (3, 1, 1) Plot (t, x) X label (t); Y label (x(t));
Title (unit ramp signal) Subplot (3,1, 2); Stem (t, x); X label (t); Y label (x (t)); Title (unit ramp sequence) Y sinusoidal T=2 F=1/T X=sin(2*pi*f*t); Figure(3) Subplot(3,1,1); Plot(t,x); X label(t); Y label(x(t)); Title (sinusoidal signal) Subplot (3,1,2); Stem(t,x); x label(t); y label (x(t));
title (sinusoidal sequence); % sine signal X= sinc(t) Subplot (3,1,3); Plot (t,x); X label(t); Y label(x(t)); Title (sinc signal); % exponential signal A=2 B=0.6 X =A*exp(b*t); Figure (4); Subplot (3,1,1); Plot (t,x); X label (t); Y label (x(t)); Title (exponential increasing signal); Subplot (3,1,2); Plot (t,x);
X label (t); Y label (x(t)); Title (exponentilal increasing sequence); % square wave signal X= square (t) Figure(5); Subplot (3,1,1); Plot (t,x); X label (t); Y label (x(t)); Title (square wave signal); Subplot (3,1,2); Plot (t,x); X label (t); Y label (x(t)); Title (square wave sequence); % triangular wave signal X= triputs(t) Figure (6) Subplot (3,1,1);
Plot (t,x); X label (t); Y label (x(t)); Title (triangular pulse sequence); % sawtooth signal X= 6*sawtooth(t) Figure (t); Subplot (3,1,1); Plot (t,x); X label (t); Y label (x(t)); Title (sawtooth signal); Subplot (3,1,2); Plot (t,x); X label (t); Y label (x(t)); Title (sawtooth sequence);
AIM: To develop a MATLAB code for frequency response of First order system and to verify it using MATLAB Software. Apparatus Required: MATLAB Software 10
Program: Clc;
Close all; Clear all; J= sqrt(-1) W=[ ]; mag-H1=[ ] ; mag H2 [ ]; pha- H1 =[ ]; pha-H2 = [ ] For w1 = -pi : 0.01: pi H1 =1/(1-0.5* exp (-j* w1)) H2 = 1/(1+ 0.5* exp(-j*w1)) H1-M = abs(H1) H2-M = abs(H2) H1-P = angle(H1) H2-P = angle (H2) Mag H1 = [ mag-H1 , H1-M] Mag H1 = [ mag-H1 , H1-M] pha H1 = [ pha-H1 , H1-p] pha H2 = [ pha-H2 , H2-p] w=[w,w1] end
figure(1) subplot(2,2,1) plot(w,mag-H1) x label(w in rad) y label (magnitude of H1(jw)) subplot(2,2,1) plot(w,mag-H1) x label(w in rad) y label (magnitude of H1(jw)) subplot(2,2,3) plot(w,pha-H1) x label(w in rad) y label (phase of H1(jw) in rad) subplot(2,2,4) plot(w,pha-H2) x label(w in rad) y label (phase of H2(jw) in rad)
AIM: To develop a MATLAB code for autocorrelation & power density spectrum. Apparatus Required: MATLAB Software 10
Program:
Clc; Close all; Clear all; X= input (enter the seq) N= length (X) n= 0:1:N-1 y= X corr (X,X) subplot (3,1,1) stem ( n,x) X label (n>); Y label ( amplitude..>); Title (i/p seq) Subplot (3,1,2) N = length (y) n= 0:1:N-1 stem ( n,y) X label (n>);
Y label ( amplitude..>); Title (autocorr seq for i/p) Disp (autocorr seq for i/p) Disp(y) P= fft(Y,N) Subplot(3,1,3); figure(2) subplot (1,1,1) stem ( n,p) x label (k); y label ( amplitude..>); Title (PSD of i/p) disp (the PSD fun:) disp(p)
lets start off with a simple cosine wave, written in the following manner:
Next, lets generate this curve within matlab using the following commands:
fo = 4; %frequency of the sine wave Fs = 100; %sampling rate Ts = 1/Fs; %sampling time interval t = 0:Ts:1-Ts; %sampling period n = length(t); %number of samples y = 2*sin(2*pi*fo*t); %the sine curve %plot the cosine curve in the time domain sinePlot = figure; plot(t,y) xlabel('time (seconds)') ylabel('y(t)') title('Sample Sine Wave') grid
When we take the fft of this curve, we would ideally expect to get the following spectrum in the frequency domain (based on fourier theory, we expect to see one peak of amplitude 1 at -4 Hz, and another peak of amplitude 1 at +4 Hz):
%DFT close all; clear all; N=input('Howmany point DFT do you want?'); x2=input('Enter the sequence='); n2=length(x2); c= zeros(N); x2=[x2 zeros(1,N-n2)]; for k=1:N for n=1:N w=exp((-2*pi*i*(k-1)*(n-1))/N); %prev.step=>evaluating w-matrix x(n)=w; end c(k,:)=x; end r=[c]*[x2'] %plotting magnitude and angle subplot(211) stem(abs(r)); title('DFT-absolute value'); subplot(212) stem(angle(r)); title('DFT-angle'); OUTPUT WAVEFORM
Program : FFT.m % Purpose : To calculate FFT of a sine modulated Gauss function % ---------------------------------------------------------------------------------------% Get the input parameters N=input ('N = ? '); fr=input('Modulation frequency [Hz] = ? '); dt=input ('time step, dt [s] = ? '); w=2*pi*fr; % Build the input time series for k=1:N X(k)=sin(w*dt*k)*exp(-(dt*N/(10*dt*k))^2); end % Apply the FFT X2=fft(X,N); X2=fftshift(X2); X2=abs(X2); % Prepare the frequency samples fmax=1/dt; df=1/(N*dt); for k=1:N F(k)=-fmax/2+(k-1)*df; end % Plot the output plot(F,X2); title('The FFT of a Time Signal') xlabel('Frequency [Hz]'); ylabel('Amplitude') % swaps the left and right halves
% -----------------------------------------------------------------------------------% Get the input parameters fr1=input('Frequency of the first sinusoid [Hz] = ? '); a1=input('Amplitude of the first sinusoid [ ] = ? '); fr2=input('Frequency of the second sinusoid [Hz] = ? '); a2=input('Amplitude of the first sinusoid [ ] = ? '); T=input ('Time record length [s] = ? '); dt=input ('sampling time interval [s] = ? '); fmax=input('maximum frequency for DFT [Hz] = ? '); df=input('frequency sampling interval for DFT [Hz] = ? ');
N=T/dt; w1=2*pi*fr1; w2=2*pi*fr2; M=fmax/df; % Build the input time series for k=1:N st(k)=a1*sin(w1*dt*k)+a2*sin(w2*dt*k); end % Apply the DFT with M points for k=1:M Sf(k)=complex(0,0); for n=1:N Sf(k)=Sf(k)+st(n)*exp(-i*2*pi*n*dt*k*df); end Sf(k)=Sf(k)*dt/2*pi; end % Prepare the frequency samples
for k=1:M F(k)=(k-1)*df; end % Plot the output plot(F,abs(Sf)); title('The DFT of the Sum of two Sinusoids') xlabel('Frequency [Hz]'); ylabel('Amplitude') %------------------------------ End of DFT.m ------------------------------------
Program:
Result
%LINEAR CONVOLUTION x=input('enter input sequence'); h=input('enter impulse response'); y=conv(x,h); subplot(3,1,1); stem(x); ylabel('amplitude'); xlabel('a'); title('input signal'); subplot(3,1,2); stem(h); ylabel('amplitude'); xlabel('b'); title('impulse response'); subplot(3,1,3); stem(y); ylabel('amplitude'); xlabel('c'); title('linear conv') disp('the resultant signal is');