Top Banner
PROGRAM1 To compute & plot the convolution of the sequences x(n) & y(n) using DFT & IDFT where x(n)=[3 4 2 11 0 7 -1 10 3] & h(n)=[1.2 3.7 4.8] x=[3 4 2 11 0 7 -1 10 3]; h=[1.2 3.7 4.8]; xlen=length(x); %find the length of sequence hlen=length(h); ylen=xlen+hlen-1; % compute the resulting length of output sequence N=ylen; xk=fft(x,N); % compute the DFTs of x[n] & y[n] hk=fft(h,N); yk=xk.*hk; % Note the use of '.*' -> Vector multiplication y=ifft(yk); % convolution o/p stem(y); figure(1);
31
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: dspa

PROGRAM1To compute & plot the convolution of the sequences x(n) & y(n) using DFT & IDFT where x(n)=[3 4 2 11 0 7 -1 10 3] & h(n)=[1.2 3.7 4.8]

x=[3 4 2 11 0 7 -1 10 3];h=[1.2 3.7 4.8];

xlen=length(x); %find the length of sequencehlen=length(h);ylen=xlen+hlen-1; % compute the resulting length of output sequenceN=ylen;xk=fft(x,N); % compute the DFTs of x[n] & y[n]hk=fft(h,N);yk=xk.*hk; % Note the use of '.*' -> Vector multiplicationy=ifft(yk); % convolution o/pstem(y);figure(1);

Page 2: dspa

PROGRAM1 - WAVEFORM

Result:y =

3.6000 15.9000 31.6000 39.8000 50.3000 61.2000 24.7000 41.9000 35.8000 59.1000 14.4000

Page 3: dspa

PROGRAM2

To compute & plot the convolution of the sequences x(n) & y(n) using ‘conv’ command where x(n)=[3 4 2 11 0 7 -1 10 3] & h(n)=[1.2 3.7 4.8]

x=[3 4 2 11 0 7 -1 10 3];h=[1.2 3.7 4.8];y=conv(x,h);stem(y);figure(1);

Result:y =

3.6000 15.9000 31.6000 39.8000 50.3000 61.2000 24.7000 41.9000 35.8000 59.1000 14.4000

Page 4: dspa

PROGRAM 3

To compute & plot the sequence x(n) & y(n) using 9 point DFT & IDFT where x(n)=[3 4 2 11 0 7 -1 10 3] & h(n)=[1.2 3.7 4.8] (example of circular convolution which is linear convolution with aliasing)

x=[3 4 2 11 0 7 -1 10 3];h=[1.2 3.7 4.8];xk=fft(x,9);hk=fft(h,9);yk=xk.*hk;y=ifft(yk);stem(y);figure(1);

Resulty = 62.7000 30.3000 31.6000 39.8000 50.3000 61.2000 24.7000 41.9000 35.8000Note: y = 3.6000 15.9000 31.6000 39.8000 50.3000 61.2000 24.7000 41.9000 35.8000 59.1000 14.400062.7 = 59.1+3.6; 30.3 = 14.4 + 15.9—aliasing effect

Page 5: dspa

PROGRAM 4

To compute & plot the power spectrum for the signalx[n]=sin(2pi0.315n)+cos2pi(0.315+0.06)n :n=0,1....8 using 128 pt DFT

n=0:15;x=sin(2*pi*0.315*n)+cos(2*pi*(0. 315+0.06)*n);N=128;xk=fft(x,N);p=xk.*conj(xk)/N;stem(p);

Page 6: dspa

PROGRAM 5 - WAVEFORM

Page 7: dspa

PROGRAM 5To compute & plot the power spectrum for the signal x[n]=sin(2pi0.315n)+cos(2pi(0.315+0.06)n) using

8 pt DFT

n=0:15;x=sin(2*pi*0.315*n)+cos(2*pi*(0.315+0.06)*n);N=8;xk=fft(x,N);p=xk.*conj(xk)/N;stem(p);

Page 8: dspa

PROGRAM 6

To plot the magnitude & phase response of IIR filter described by transfer equation y(n)=0.8y(n-1) + 0.2x(n)

b=[0.2];a=[1 -0.8];%finding the frequency response[h,w]=freqz(b,a,128);%plotting the mag responsefigure(1)plot(w,abs(h));%plotting the phase responsefigure(2)plot(w,angle(h));

Page 9: dspa

PROGRAM 6 – WAVEFORM 1

Page 10: dspa

PROGRAM 6 – WAVEFORM 2

Page 11: dspa

PROGRAM 7

Design of a second order butterworth filter with cutoff frequency

[a,b]=butter(2,0.2)%finding the frequency response[h,w]=freqz(a,b,512);%plotting the mag responsefigure(1)plot(w,abs(h))%plotting the phase responsefigure(2)plot(w,angle(h))

Page 12: dspa

PROGRAM 7 – WAVEFORM 1-magnitude response

Page 13: dspa

PROGRAM 7 – WAVEFORM 2-phase response

Page 14: dspa

Design and implementation of IIR filter to meet given specifications

• [N, Wn] = BUTTORD(Wp, Ws, Rp, Rs)returns the order N of the lowest order digital

• [B,A] = BUTTER(N,Wn) designs an Nth order lowpass digital Butterworth filter and returns the filter coefficients in length N+1 vectors B (numerator) and A (denominator).

Page 15: dspa

MATLAB PROGRAM FOR IIR FILTERGeneration of input sequencesc=3; % eg of 3 frequenciesf(1)=20; f(2)=8;f(3)=35;fs=100; % sampling frequenciesn=1:20; % 20 sampless=[ ];s1=[ ];for i=1:c % sine waves1=sin(2*3.14*n*f(i)/fs); % appending the 3 frequenciess=[s s1]; end% figure of input sequencesfigure(1)stem(s) title(input)

%To design a IIR filter%[b,a]=butter(n,Wc,type)

% generate filter co-effs %n=order ,Wc=cut off=fc/fmax %fmax =fs/2, type=filter type%20/50=0.4

[b,a]=butter(1, 0.4)

% iir filteringOp=filter(b,a,s);

figure(3)stem(Op) % filter o/ptitle(‘filter o/p’)

Page 16: dspa
Page 17: dspa

YulewalkAlgorithm Yulewalk performs a least-squares fit in the time domain. It computes

the denominator coefficients using modified Yule-Walker equations, with correlation coefficients computed by inverse Fourier transformation of the specified frequency response.

To compute the numerator, yulewalk takes the following steps:• Computes a numerator polynomial corresponding to an additive

decomposition of the power frequency response.• Evaluates the complete frequency response corresponding to the

numerator and denominator polynomials.• Uses a spectral factorization technique to obtain the impulse

response of the filter.• Obtains the numerator polynomial by a least-squares fit to this

impulse response.

Page 18: dspa

Design an 8th-order lowpass filter and overplot the desired frequencyresponse with the actual frequency response:

f = [0 0.6 0.6 1];m = [1 1 0 0];[b,a] = yulewalk(8,f,m);[h,w] = freqz(b,a,128);plot(f,m,w/pi,abs(h),'--')legend('Ideal','yulewalk Designed')title('Comparison of Frequency Response Magnitudes')

Page 19: dspa

Design and implementation of FIR filter to meet given

specifications• B = FIR1(N,Wn) designs an N'th order

lowpass FIR digital filter and returns the filter coefficients in length N+1 vector B.

• [H,W] = FREQZ(B,A,N) returns the N-point complex frequency response vector H and the N-point frequency vector W in radians/sample of the filter whose numerator and denominator coefficients are in vectors B and A.

Page 20: dspa

MATLAB PROGRAM FOR FIR FILTER%Generation of input sequencesc=3; % eg of 3 frequenciesf(1)=20; f(2)=8;f(3)=35;% sampling frequencyfs=100; n=1:20; % 20 sampless=[ ];s1=[ ];for i=1:c% sine waves1=sin(2*3.14*n*f(i)/fs);% appending the 3 frequenciess=[s s1]; endfigure(1)%stem(s)

% figure of input sequencen1=1:60;plot(n1,s)title('input')

%d=fir1(n,Wc,type) % generate filter co-effs n=order ,Wc=cut off ,type=filter type

d=fir1(16, 20/50,blackman(17))figure(2) % filter coeffs graphstem(d) title('filtergraph')

% convolutionOp=conv(d,s); figure(3)%stem(Op) % filter o/pn2=1:length(Op);plot(n2,Op)title('filter o/p')

figure(4)%fir window coeffsstem(Blackman(17)) title('fir filter window used')

Page 21: dspa
Page 22: dspa

Parks-McClellan algorithm• The Remez FIR Filter Design block implements

the Parks-McClellan algorithm to design and apply a linear-phase filter with an arbitrary multiband magnitude response.

• The filter design, which uses the firpm function in Signal Processing Toolbox, minimizes the maximum error between the desired frequency response and the actual frequency response.

• Such filters are called equiripple due to the equiripple behavior of their approximation error.

• The block applies the filter to a discrete-time input using the Direct-Form II Transpose Filter block.

Page 23: dspa

%filter specificationsf=[0 .1 .2 .3 .4 .6 .7 .8 .9 1];m=[0 0 1 1 1 1 0 0 0 0];n=20; %filter orderb = remez(n,f,m);[h,w] = freqz(b,1,128);plot(f,m,w/pi,abs(h),'--')legend('Ideal','Parks McClellen Designed')title('Comparison of Frequency Response Magnitudes')

Page 24: dspa

Interpolation

• Increases the sampling rate-L*fs where L-Interpolation factor

• Involves adding the zero samples (L-1) and• Filtering to remove image frequencies

Up Sampler

Digital Low passfilter (π/L)

X[n]

fs L*fsL*fs

Y[n]

Page 25: dspa

MATLAB program to implement Interpolation

%Generation of input sequences

fs=1000; %Sampling frequencyi=1:20;s=sin(2*pi*i*100/fs); figure(1)n2=1:length(s);plot(n2,s)title('input')f=3 %Interpolation factorintop=[];for i=1:20intop=[intop s(i) 0 0];end

%d=fir1(20,fs/(f*500))d=[1/3 2/3 1 2/3 1/3];op=conv(d,intop);figure(2)n1=1:length(op);plot(n1,op)title('output after filtering')

figure(3)n3=1:length(intop);plot(n3,intop)title('interpolated output')

Page 26: dspa

With d=fir1(20,fs/(f*500))

With d=[1/3 2/3 1 2/3 1/3];

Page 27: dspa

Decimation• Decreases the sampling rate-fs/D • where D-decimation factor• Involves dropping the samples (D-1) without violating the

sampling theorem

Digital Low passfilter (π/D)

DownSampler

X[n]

fs fs Fs/D

Y[n]

Page 28: dspa

MATLAB program to implement Decimation

clear%Generation of input sequencesfs=1000; %Sampling frequencyi=1:60;s=sin(2*pi*i*100/fs); % sine wavefigure(1)n2=1:length(s);plot(n2,s)%stem(s) % figure of input sequencestitle('input')

%d=fir1(n,Wc,type) % generate filter co-effs n=order,%Wc=cut-off,type=filter type%Eg. d=fir1(16, 0.15,blackman(17))%Wc=cut-off=fs/decimation factor %Normalized wc=Wc/fmax where fmax=fs/2df=3; %decimation factorfc=1000/(df*500) %cut off freq of deci filterd=fir1(5,fc);figure(2)stem(d) % filter coeffs graphtitle('filtergraph')

op=conv(d,s); % convolutionfigure(3)stem(op) % filter o/pdecop=[];for i=1:df:65decop=[decop op(i)];endfigure(4)%stem(decop)n=1:length(decop)plot(n,decop) title('decimated output by a factor 3')figure(5);stem(abs(fft(op,256)))

figure(6);stem(abs(fft(decop,256)))title('fft of decimated output')figure(7);stem(abs(fft(s,256)))title('fft of input')

Page 29: dspa

Figures with a decimation filter of order 5 and 15

Page 30: dspa

MATLAB program to implement Decimation

tfinal=0.05;t=0:0.00005:tfinal;fd=input('Enter analog frequency');%define analog signal for comparison xt=cos(2*pi*fd*t);

%simulate condition for undersamplingi.e., fs1<2*fdfs1=1.3*fd; %define the time vectorn1=0:1/fs1:tfinal;%Generate the undersampled signal

xn=cos(2*pi*n1*fd);

%plot the analog & sampled signalssubplot(3,1,1);plot(t,xt,'b',n1,xn,'r*-');title('undersampling plot');

%condition for Nyquist plotfs2=2*fd;n2=0:1/fs2:tfinal;xn=cos(2*pi*fd*n2);subplot(3,1,2);plot(t,xt,'b',n2,xn,'r*-');title('Nyquist plot');

%condition for oversamplingfs3=5*fd; n3=0:1/fs3:tfinal;xn=cos(2*pi*fd*n3);subplot(3,1,3);plot(t,xt,'b',n3,xn,'r*-');title('Oversampling plot');xlabel('time');ylabel('amplitude');legend('analog','discrete');

Page 31: dspa