Showing posts with label EC2306 -DIGITAL SIGNAL PROCESSING. Show all posts
Showing posts with label EC2306 -DIGITAL SIGNAL PROCESSING. Show all posts

Saturday, 17 September 2011

IIR filter


IIR FILTER DESIGN

clc;
close all;
clear all;
rp=input('enter the pass band ripple');
rs=input('enter the stop band ripple');
wp=input('enter the pass band freq');
ws=input('enter the stop band freq');
fs=input('enter the sampling feq');
w1=2*wp/fs;
w2=2*ws/fs;
[h,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(h,wn);
w=0:.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('gain in db....>');
xlabel('(a) normalised frequency>');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b) normalised frequency');
ylabel('phase in radian');

cicular convolution


CIRCULAR CONVOLUTION

clc;
clear all;
close all;
g=input('1 sequence');
h=input('2 sequence');
N1=length(g);
N2=length(h);
N=max(N1,N2);
N3=N1-N2;
if(N3>=0)
    h=[h,zeros(1,N3)];
else
    g=[g,zeros(1,-N3)];
end
for n=1:N
    y(n)=0;
    for i=1:N
        j=n-i+1;
        if(j<=0)
            j=N+j;
        end
    end
    y(n)=[y(n)+g(i)*h(j)];
    stem(y)
end

Hamming windown

 FIR FILTER USING HAMMING WINDOW
clc; clear all; close all
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
fp=input('enter the passband freq');
fs=input('enter the stopband freq');
f=input('enter the sampling freq');
wp=2*fp/f;ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0);
    n1=n;
    n=n-1;
end
y=hamming(n1);
%LOW -PASS FILTER
b=fir1(n,wp,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);plot(o/pi,m);
ylabel('gain in db-->');
xlabel('(a)normalised frequency-->');
title('LOW-PASS FILTER');
%BAND PASS FILTER
wn=[wp ws];
b=fir1(n,wn,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,3);plot(o/pi,m);
ylabel('gain in db-->');
xlabel('(c)normalised frequency-->');
title('BAND PASS FILTER');

FIR filter


FIR FILTER USING RECTANGULAR WINDOW
 clc;close all;clear all;
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
fp=input('enter the passband ripple');
fs=input('enter the stopband ripple');
f=input('enter the sampling frequency');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rs*rp))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
if(rem(n,2)~=0)
    n=n1;
    n=n-1;
end
y=RECTWIN(n1);
%bandpass filter
wn=[wp ws];
b=fir1(n,wn,y);
[h,o]=freqz(b,1,256);
m=20*log10*(abs(h));
an=angle(h);
subplot(2,1,1);
plot(o/pi,m);
xlabel('normalised freq-->');
ylabel('gain in db-->');
subplot(2,1,2);
plot(0/pi,an);
xlabel('normalised freq-->');
ylabel('phase in radians-->');

frequency domain


Program P10 4 can be employed to study the frequency-domain properties of the downsampler.
% Program P10_4
% Effect of Down-sampling in the Frequency Domain
% Use fir2 to create a bandlimited input sequence
clf;
freq = [0 0.42 0.48 1]; mag = [0 1 0 0];
x = fir2(101, freq, mag);
% Evaluate and plot the input spectrum
[Xz, w] = freqz(x, 1, 512);
subplot(2,1,1);
plot(w/pi, abs(Xz)); grid
xlabel(’\omega/\pi’); ylabel(’Magnitude’);
title(’Input Spectrum’);
% Generate the down-sampled sequence
M = input(’Type in the down-sampling factor = ’);
y = x([1: M: length(x)]);
% Evaluate and plot the output spectrum
[Yz, w] = freqz(y, 1, 512);
subplot(2,1,2);
plot(w/pi, abs(Yz)); grid
xlabel(’\omega/\pi’); ylabel(’Magnitude’);
title(’Output Spectrum’);

Band limited input sequence


Use fir2 to create a bandlimited input sequence
clf;
freq = [0 0.45 0.5 1];
mag = [0 1 0 0];
x = fir2(99, freq, mag);
% Evaluate and plot the input spectrum
[Xz, w] = freqz(x, 1, 512, ’whole’);
subplot(2,1,1);
plot(w/pi, abs(Xz)); axis([0 1 0 1]); grid
xlabel(’\omega/\pi’); ylabel(’Magnitude’);
title(’Input Spectrum’);
subplot(2,1,2);
% Generate the up-sampled sequence
L = input(’Type in the up-sampling factor = ’);
y = zeros(1, L*length(x));
y([1: L: length(y)]) = x;
% Evaluate and plot the output spectrum
[Yz, w] = freqz(y, 1, 512, ’whole’);
plot(w/pi, abs(Yz)); axis([0 1 0 1]); grid
xlabel(’\omega/\pi’); ylabel(’Magnitude’);
title(’Output Spectrum’);

down sampling


Illustration of Down-Sampling by an Integer Factor

clf;
n = 0: 49;
m = 0: 50*3 - 1;
x = sin(2*pi*0.042*m);
y = x([1: 3: length(x)]);
subplot(2,1,1)
stem(n, x(1:50)); axis([0 50 -1.2 1.2]);
title(’Input Sequence’);
xlabel(’Time index n’);
ylabel(’Amplitude’);
subplot(2,1,2)
stem(n, y); axis([0 50 -1.2 1.2]);
title(’Output Sequence’);
xlabel(’Time index n’);
ylabel(’Amplitude’);

samplar


Program up-sampler.

% Illustration of Up-Sampling by an Integer Factor
%
clf;
n = 0:50;
x = sin(2*pi*0.12*n);
y = zeros(1, 3*length(x));
y([1: 3: length(y)]) = x;
subplot(2,1,1)
stem(n,x);
title(’Input Sequence’);
xlabel(’Time index n’);ylabel(’Amplitude’);
subplot(2,1,2)
stem(n,y(1:length(x)));
title(’Output Sequence’);
xlabel(’Time index n’);ylabel(’Amplitude’);

casual IIR filter


4. Program illustrates the design of a causal IIR filter, its simulation in transposed Direct Form II, and its application in filtering a signal.
% Program P8_3
% Illustration of Filtering by an IIR Filter
%
clf;
% Generate the input sequence
k = 0:50;
w2 = 0.7*pi;w1 = 0.2*pi;
x1 = 1.5*cos(w1*k); x2 = 2*cos(w2*k);
x = x1+x2;
% Determine the filter transfer function
[N, Wn] = ellipord(0.25, 0.55, 0.5, 50);
[num, den] = ellip(N,0.5, 50,Wn);
% Generate the output sequence
y = filter(num,den,x);
% Plot the input and the output sequences
subplot(2,1,1);
stem(k,x); axis([0 50 -4 4]);
xlabel(’Time index n’); ylabel(’Amplitude’);
title(’Input Sequence’);
subplot(2,1,2);
stem(k,y); axis([0 50 -4 4]);
xlabel(’Time index n’); ylabel(’Amplitude’);
title(’Output Sequence’);

IIR filter

To implement a causal IIR filter implemented in the Direct Form II structure, the function direct2 given below can be employed.
function [y,sf] = direct2(p,d,x,si);
% Y = DIRECT2(P,D,X) filters input data vector X with
% the filter described by vectors P and D to create the
% filtered data Y. The filter is a "Direct Form II"
% implementation of the difference equation:
% y(n) = p(1)*x(n) + p(2)*x(n-1) + ... + p(np+1)*x(n-np)
% - d(2)*y(n-1) - ... - d(nd+1)*y(n-nd)
% [Y,SF] = DIRECT2(P,D,X,SI) gives access to initial and
% final conditions, SI and SF, of the delays.
dlen = length(d); plen = length(p);
N = max(dlen,plen); M = length(x);
sf = zeros(1,N-1); y = zeros(1,M);
if nargin ~= 3,
sf = si;
end
if dlen < plen,
d = [d zeros(1,plen - dlen)];
else
p = [p zeros(1, dlen - plen)];
end
p = p/d(1); d = d/d(1);
for n = 1:M;
wnew = [1 -d(2:N)]*[x(n) sf]’;
K = [wnew sf];
y(n) = K*p’;
sf = [wnew sf(1:N-2)];
end

Butterworth filter


   illustrates the design of a Butterworth bandstop filter.
% Program P7_1
% Design of a Butterworth Bandstop Digital Filter
Ws = [0.4 0.6]; Wp = [0.3 0.7]; Rp = 0.4; Rs = 50;
% Estimate the Filter Order
[N1, Wn1] = buttord(Wp, Ws, Rp, Rs);
% Design the Filter
[num,den] = butter(N1,Wn1,’stop’);
% Display the transfer function
disp(’Numerator coefficients are ’);disp(num);
disp(’Denominator coefficients are ’);disp(den);
% Compute the gain response
[g,w] = gain(num,den);
% Plot the gain response
plot(w/pi,g);grid
axis([0 1 -60 5]);
xlabel(’\omega /\pi’); ylabel(’Gain, dB’);
title(’Gain Response of a Butterworth Bandstop Filter’);

LINEAR CONVOLUTION


EC2306                            DIGITAL SIGNAL PROCESSING LAB                      

 

USING TMS320C5X/TMS320C 67XX/ADSP 218X/219X/BS531/532/561
1.                                                  Study of various addressing modes of DSP using simple programming  examples
2.                                                  Implementation of Linear and Circular Convolution
3.                                                  Sampling of input signal and display
4.                                                  Waveform generation
5.                                                  Implementation of FIR filter

USING MATLAB
1.                                                                                                                      Generation of Signals
2.                                                                                                                      Linear and circular convolution of two sequences
3.                                                                                                                      Sampling and effect of aliasing
4.                                                                                                                      Design of FIR filters
5.                                                                                                                      Design of IIR filters
6.                                                                                                                      Calculation of FFT of a signal
7.                                                                                                                      Decimation by polyphase decomposition.



1. LINEAR CONVOLUTION

clc;clear all;close all;
x=input('ENTER THE FIRST SEQUENCE    ');
h=input('ENTER THE SECOND SEQUENCE    ');
y=conv(x,h);
stem(y);
xlabel('Amplitude---->');
ylabel('time----->');
title('LINEAR CONVOLUTION');