0% found this document useful (0 votes)
12 views

Angel Record

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Angel Record

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

PROGRAM:

%PROGRAM for sine wave


t=0:0.1:10;
y=sin(2*pi*t);
subplot(3,3,1);
plot(t,y,'k');
xlabel('Time');
ylabel('Amplitude');
title('Sine wave');
%PROGRAM for cosine wave
t=0:0.1:10;
y=cos(2*pi*t);
subplot(3,3,2);
plot(t,y,'k');
xlabel('Time');
ylabel('Amplitude');
title('Cosine wave');
%PROGRAM for square wave
t=0:0.001:10;
y=square(t);
subplot(3,3,3);
plot(t,y,'k');
xlabel('Time');
ylabel('Amplitude');
title('Square wave');
%PROGRAM for sawtooth wave
t=0:0.1:10;
y=sawtooth(t);
subplot(3,3,4);

plot(t,y,'k');
xlabel('Time');
ylabel('Amplitude');
title('Sawtooth wave');
%PROGRAM for Triangular wave
t=0:.0001:20;
y=sawtooth(t,.5); % sawtooth with 50% duty cycle(triangular)
subplot(3,3,5);
plot(t,y);
ylabel ('Amplitude');
xlabel ('Time Index');
title('Triangular waveform');
%PROGRAM for Sinc Pulse
t=-10:.01:10;
y=sinc(t);
axis([-10 10 -2 2]);
subplot(3,3,6)
plot(t,y)
ylabel ('Amplitude');
xlabel ('Time Index');
title('Sinc Pulse');
% PROGRAM for Exponential Growing signal
t=0:.1:8;
a=2;
y=exp(a*t);
subplot(3,3,7);
plot(t,y);
ylabel ('Amplitude');
xlabel ('Time Index'); title('Exponential growing Signal');

% PROGRAM for Exponential Growing signal t=0:.1:8;


a=2;
y=exp(-a*t); subplot(3,3,8); plot(t,y);
ylabel ('Amplitude');
xlabel ('Time Index');
title('Exponential decaying Signal');
OUTPUT:
PROGRAM:

%PROGRAM for unit step sequence


clc;
N=input('Enter the length of unit step sequence(N)= ');
n=0:1:N-1;
y=ones(1,N);
subplot(3,2,1);
stem(n,y,'k');
xlabel('Time')
ylabel('Amplitude')
title('Unit step sequence');
%PROGRAM for unit ramp sequence
N1=input('Enter the length of unit ramp sequence(N1)= ');
n1=0:1:N1-1;
y1=n1;
subplot(3,2,2);
stem(n1,y1,'k');
xlabel('Time');
ylabel('Amplitude');
title('Unit ramp sequence');
%PROGRAM for sinusoidal sequence
N2=input('Enter the length of sinusoidal sequence(N2)=');
n2=0:0.1:N2-1;
y2=sin(2*pi*n2);
subplot(3,2,3);
stem(n2,y2,'k');
xlabel('Time');
ylabel('Amplitude');
title('Sinusoidal sequence');

%PROGRAM for cosine sequence


N3=input('Enter the length of the cosine sequence(N3)=');
n3=0:0.1:N3-1;
y3=cos(2*pi*n3);
subplot(3,2,4);
stem(n3,y3,'k');
xlabel('Time');
ylabel('Amplitude');
title('Cosine sequence');
%PROGRAM for exponential sequence
N4=input('Enter the length of the exponential sequence(N4)= ');
n4=0:1:N4-1;
a=input ('Enter the value of the exponential sequence(a)= ');
y4=exp (a*n4);
subplot(3,2,5);
stem(n4,y4,'k');
xlabel('Time');
ylabel('Amplitude');
title('Exponential sequence');
%PROGRAM for unit impulse
n=-3:1:3;
y=[zeros(1,3),ones(1,1),zeros(1,3)];
subplot(3,2,6);
stem(n,y,'k');
xlabel('Time');
ylabel('Amplitude ');
title('Unit impul sequence');
OUTPUT:
PROGRAM:

clc;
clear all;
close all;
x=input('Enter the sequence 1: ');
h=input('Enter the sequence 2: ');
y=xcorr(x,h);
figure;
subplot(3,1,1);
stem(x);
xlabel('n->');
ylabel('Amplitude->');
title('Input sequence 1');
subplot(3,1,2);
stem(fliplr(y));
stem(h);
xlabel('n->');
ylabel('Amplitude->');
title('Input sequence 2');
subplot(3,1,3);
stem(fliplr(y));
xlabel('n->');
ylabel('Amplitude->');
title('Output sequence');
disp('The resultant is');
fliplr(y);
OUTPUT:

(Cross-Correlation of the Sequences)

Enter the sequence 1: [1 3 5 7]


Enter the sequence 2: [2 4 6 8]

(Auto Correlation Function)


Enter the sequence [1 2 3 4]
PROGRAM:

clc;
close all;
clear all;
x=input('Enter x: ');
h=input('Enter h: ') ;
m=length(x);
n=length(h);
X=[x,zeros(1,n)];
H=[h,zeros(1,m)];
for i=1:n+m-1
Y(i)=0;
for j=1:i
Y(i)=Y(i)+X(j)*H(i-j+1);
end
end
stem(Y);
disp(Y);
ylabel('Y[n]');
xlabel('----->n') ;
title('Convolutio n of Two Signals without conv function');

INPUT:
Enter x: [1 2 3 4 5]
x=12345
Enter h: [1 2 3 1]
h=1231
Y = 1 4 10 17 24 25 19 5
OUTPUT:
PROGRAM:

clc;
clear all ;
close all;
a = input('Enter the sequence x(n) = ');
b = input('Enter the sequence h(n) = ');
n1=length(a);
n2=length(b);
N=max(n1,n2);
x = [a zeros(1,(N-1))];
for i = 1:N
k = i;
for j = 1:n2

H(i,j)=x(k)*b(j);
k = k-1;
if(k == 0)
k = N;
end
end
end
y=zeros(1,N);
M=H';
for j = 1:N
for i = 1:n2
y(j)=M(i,j)+y(j);
end
end
disp('The output sequence is y(n)=');
disp(y);
stem(y);
title('Circular Convolution');
xlabel('n');
ylabel('y(n)');
INPUT:

Enter the sequence x(n) = [1 2 3 4]

x(n) = 1 2 3 4

Enter the sequence h(n) = [1 2 1 1]

h(n) = 1 2 1 1

The output sequence is y(n)=

14 11 12 13

OUTPUT:
PROGRAM : (Spectrum Analysis Using DFT)
clc;
clear all ;
close all;
N=input('type length of DFT= ');
T=input('type sampling period= ');
freq=input('type the sinusoidal freq= ');
k=0:N-1;
f=sin(2*pi*freq*1/T*k);
F=fft(f);
stem(k,abs(F));
grid on;
xlabel('k');
ylabel('X(k)');

Output:
(Spectrum Analysis Using DFT)
Type length of dft=32
Type sampling period=64
Type the sinusoidal freq=11
PROGRAM : (Rectangular Window)
clc;
clear all ;
close all;
rp=input('Enter the PB ripple rp =');
rs=input('Enter the SB ripple rs =');
fp=input('Enter the PB ripple fp =');
fs=input('Enter the SB ripple fs =');
f=input('Enter the sampling frequency f =');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
den=14.6*(fs-fp)/f;
n=ceil(num/den);
n1=n+1;
if(rem(n,2)~=0)
n=n1;
n=n-1;
end;
y=boxcar(n1);
%LPF
b=fir1(n,wp,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(o/pi,m);
xlabel('Normalized frequency------>'); ylabel('Gain in db--------.');

title('MAGNITUDE RESPONSE OF LPF');

%HPF

b=fir1(n,wp,'high',y);

[h,o]=freqz(b,1,256);
m=20*log10(abs(h));

subplot(2,2,2);

plot(o/pi,m);

xlabel('Normalized frequency------>');

ylabel('Gain in db--------.');

title('MAGNITUDE RESPONSE OF HPF');

%BPF

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);

xlabel('Normalized frequency------>');

ylabel('Gain in db--------.');

title('MAGNITUDE RESPONSE OF BPF');

%BSF

b=fir1(n,wn,'stop',y);

[h,o]=freqz(b,1,256);

m=20*log10(abs(h));

subplot(2,2,4);

plot(o/pi,m);

ylabel('Gain in db--------.');

title('MAGNITUDE RESPONSE OF LPF');


%HPF

b=fir1(n,wp,'high',y);

[h,o]=freqz(b,1,256);

m=20*log10(abs(h));

subplot(2,2,2);

plot(o/pi,m);

xlabel('Normalized frequency------>');

ylabel('Gain in db--------.');

title('MAGNITUDE RESPONSE OF HPF');

%BPF

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);

xlabel('Normalized frequency------>');

ylabel('Gain in db--------.');

title('MAGNITUDE RESPONSE OF BPF');

%BSF

b=fir1(n,wn,'stop',y);

[h,o]=freqz(b,1,256);

m=20*log10(abs(h));

subplot(2,2,4);
plot(o/pi,m);

xlabel('Normalized frequency------

>');

ylabel('Gain

in db--------.');

title('MAGNITUDE RESPONSE OF BSF');

Output:

Enter the PB ripple Rp =.03

Enter the SB ripple rs =.05

Enter the PB ripple fp =2000

Enter the SB ripple fs =3000

Enter the Sampling frequency f =9000


PROGRAM : (Rectangular Window)
clc;
clear all ;
close all;
rp=input('Enter the PB ripple rp =');
rs=input('Enter the SB ripple rs =');
fp=input('Enter the PB ripple fp =');
fs=input('Enter the SB ripple fs =');
f=input('Enter the sampling frequency f =');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
den=14.6*(fs-fp)/f;
n=ceil(num/den);
n1=n+1;
if(rem(n,2)~=0)
n=n1;
n=n-1;
end;
y=hanning(n1);
%LPF
b=fir1(n,wp,y);
[h,O]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(O/pi,m);
xlabel('Normalized freqency------>');
ylabel('Gain in db--------.');
title('MAGNITUDE RESPONSE OF LPF');
%HPF
b=fir1(n,wp,'high',y);
[h,O]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2);
plot(O/pi,m);
xlabel('Normalized freqency------>');
ylabel('Gain in db--------.');
title('MAGNITUDE RESPONSE OF HPF');
%BPF
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);
xlabel('Normalized freqency------>');
ylabel('Gain in db--------.');
title('MAGNITUDE RESPONSE OF BPF');
%BSF
b=fir1(n,wn,'stop',y);
[h,O]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,4);
plot(O/pi,m);
xlabel('Normalized freqency------>');
ylabel('Gain in db--------.');
title('MAGNITUDE RESPONSE OF BSF');
Output(Hanning Window)
Enter the PB ripple rP =.03
Enter the SB ripple rs=.02
Enter the PB ripple fp =1500
Enter the SB ripple fs =2000
Enter the Sampling frequency f =9000
PROGRAM : (IIR Butterworth Filter using Impulse Method)
clc;
clear all ;
close all;
N=input('ENTER THE FILTER ORDER N = ');
fs=input('ENTER THE SAMPLING FREQUENCY fs = ');
fc=input('ENTER THE CUT-OFF FREQUENCY fc = ');
wc=2*pi*fc;
[na,da]=butter(N,wc,'s');
[n,d]=impinvar(na,da,fs);
[h,f]=freqz(n,d,512,fs);
gain=20*log10(abs(h));
subplot(2,1,1);
plot(f,gain);
xlabel('Frequency---->');
ylabel('Magnitude---->');
title('AMPLITUDE RESPONSSE');
subplot(2,1,2);
zplane(n,d);
z=roots(n); p=roots(d);
xlabel('Real part---->');
ylabel('Imaginary part---->');
title('POLE-ZERO PLOT');
Output: (IIR Butterworth Filter using Impulse Method)
Enter the filter order N = 2
Enter the Sampling Frequency fs =12800
Enter the cut off frequency fc = 150
PROGRAM : (IIR Butterworth Filter using Bilinear Transformation)
clc;
clear all ;
close all;
wp=input('ENTER THE PASSBAND EDGE FREQUENCIES wp= ');
ws=input('ENTER THE STOPBAND EDGE FREQUENCIES ws= ');
rp=input('ENTER THE PASSBAND RIPPLE rp= ');
rs=input('ENTER THE STOPBAND RIPPLE rs= ');
fs=input('ENTER THE SAMPLING FREQUENCY fs= ');
wpn=wp/(fs/2);
wsn=ws/(fs/2);
[N,fc]=buttord(wpn,wsn,rp,rs);
disp('ORDER OF THE FILTER');
disp(N);
[n,d]=butter(N,wpn);
[h,f]=freqz(n,d,512,fs);
gain=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(f,gain);
xlabel('FREQUENCY---->');
ylabel('MAGNITUDE');
title('AMPLITUDE RESPONSE');
subplot(2,1,2);
zplane(n,d);
z=roots(n);
p=roots(d);
xlabel('RREAL PART---->');
ylabel('IMAGINARY PART');
title('POLE-ZERO PLOT');
Input: (IIR Butterworth Using Bilinear Transformation)
Enter the passband edge frequencies wp= [200 300]
Enter the stopband edge frequencies ws= [50 450]
Enter the passband ripple rp= 3
Enter the stopband ripple rs= 20
Enter the sampling frequency fs= 1000
Order of the filter = 2

Output:
PROGRAM : (Chebyshev Type 1 Band pass Filter)
clc;
clear all ;
close all;
alphap=2; %pass band attenuation in dB
alphas=20; %stop band attenuation in dB
wp=[.2*pi,.4*pi];
ws=[.1*pi,.5*pi];
%To find cutoff frequency and order of the filter
[n,wn]=buttord(wp/pi,ws/pi,alphap,alphas);
%system function of the filter
[b,a]=cheby1(n,alphap,wn); w=0:.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(ph/pi,m);
grid;
ylabel('Gain in dB..');
xlabel('Normalised frequency..');
subplot(2,1,2);
plot(ph/pi,an);
grid;
ylabel('Phase in radians..');
xlabel('Normalised frequency..');
Output: (Chebyshev Type 1 Band pass Filter)
PROGRAM : (Chebyshev Type II Band Reject Filter)
clc;
clear all ;
close all;
alphap=2;
alphas=20;
ws=[.2*pi,.4*pi];
wp=[.1*pi,.5*pi];
[n,wn]=cheb2ord(wp/pi,ws/pi,alphap,alphas);
[b,a]=cheby2(n,alphas,wn,'stop');
w=0:.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(ph/pi,m);
grid;
ylabel('Gain in dB..');
xlabel('Normalised frequency..');
subplot(2,1,2);
plot(ph/pi,an);
grid;
ylabel('Phase in radians..');
xlabel('Normalised frequency..');
Output: (Chebyshev Type II Band Reject Filter)
PROGRAM : (Multirate Filters)
clc;
clear all ;
close all;
N =31; % Filter length
Nord = N-1; % Filter order
L = 3;
ro1 = 0.2; % Roll-off factor
h1 = firnyquist(Nord,L,ro1); % Filter design
ro2 = 0.4; % Roll-off factor
h2 = firnyquist(Nord,L,ro2); % Filter design
ro3 = 0.6; % Roll-off factor
h3 = firnyquist(Nord,L,ro3); % filter design
figure (1)
subplot(3,1,1)
stem(0:N-1,h1,'b')
axis([0,30,-0.2,0.5])
ylabel('h_1[n]')
title('Figure 1')
legend('h1')
subplot(3,1,2)
stem(0:N-1,h2,'k')
axis([0,30,-0.2,0.5])
ylabel('h_2[n]')
legend('h2')
subplot(3,1,3)
stem(0:N-1,h3,'r')
axis([0,30,-0.2,0.5])
xlabel('n')
ylabel('h_3[n]')
legend('h3')
% Computing frequency responses
[H1,f] = freqz(h1,1,256,2);
[H2,f] = freqz(h2,1,256,2);
[H3,f] = freqz(h3,1,256,2); figure (2)
plot(f,abs(H1),'b',f,abs(H2),'k',f,abs(H3),'r'),
grid title ('Figure 2')
axis([0,1,0,1.1])
xlabel('\omega/\pi')
ylabel('Magnitude')
legend('|H_1(e^j^\omega)|','|H_2(e^j^\omega)|','|H_3(e^j^ \omega)|')
Output:
PROGRAM : (Multirate Filters)
clc;
clear all ;
close all;
M=3000;
T=2000;
dB=25;
L=20;
ChL=5;
EqD=round((L+ChL)/2);
Ch=randn(1,ChL+1)+sqrt(-1)*randn(1,ChL+1);
Ch=Ch/norm(Ch);
TxS=round(rand(1,M))*2-1;
TxS=TxS+sqrt(-1)*(round(rand(1,M))*2-1);
x=filter(Ch,1,TxS);
n=randn(1,M);
n=n/norm(n)*10^(-dB/20)*norm(x);
x=x+n; K=M-L;
X=zeros(L+1,K);
for i=1:K
X(:,i)=x(i+L:-1:i).';
end
e=zeros(1,T-10);
c=zeros(L+1,1);
mu=0.001;
for i=1:T-10
e(i)=TxS(i+10+L-EqD)-c'*X(:,i+10);
c=c+mu*conj(e(i))*X(:,i+10);
end
sb=c'*X;
sb1=sb/norm(c);
sb1=sign(real(sb1))+sqrt(-1)*sign(imag(sb1));
start=7;
sb2=sb1-TxS(start+1:start+length(sb1));
SER=length(find(sb2~=0))/length(sb2);
disp(SER);
subplot(2,2,1),
plot(TxS,'*');
grid,title('Input symbols'); xlabel('realpart'),
ylabel('imaginary part')
axis([-2 2 -2 2])
subplot(2,2,2),
plot(x,'o');
grid, title('Received samples'); xlabel('real part'),
ylabel('imaginary part')
subplot(2,2,3),
plot(sb,'o');
grid, title('Equalized symbols'),
xlabel('real part'),
ylabel('imaginary part')
subplot(2,2,4),
plot(abs(e));
grid, title('Convergence'),
xlabel('n'),
ylabel('error signal')
Output:
PROGRAM (Linear Convolution)
#include<stdio.h>
int m=6;
int n=6;
int i=0,j;
int x[15]={1,2,3,4,5,6,0,0,0,0,0,0}; int h[15]={1,2,3,4,5,6,0,0,0,0,0,0};
int y[20];
main()
{
for(i=0;i<m+n-1;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
y[i]+=x[j]*h[i-j];
}
for(i=0;i<m+n-1;i++)
printf("%d \n",y[i]);
}
OUTPUT: (Linear Convolution)
4 10 20 35 56 70 76 73 60 36
PROGRAM :(Circular Convolution)
#include<stdio.h>
int
m,n,x[30],h[30],y[30],i,j,temp[30],k,x2[30],a[30];
void main()
{
printf("enter the length of the 1st sequence\n");
scanf("%d",&m);
printf("enter the length of the second sequence\n"); scanf("%d",&n);
printf("enter the 1st sequence\n");
for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf("enter the second sequence\n");
for(j=0;j<n;j++)
scanf("%d",&h[j]);
if(m-n!=0)
{
if(m>n)
{
for(i=n;i<m;i++)
h[i]=0;
n=m;
}
for(i=m;i<n;i++)
x[i]=0;
m=n;
}
y[0]=0;
a[0]=h[0];
for(j=1;j<n;j++)
a[j]=h[n-j];
for(i=0;i<n;i++)
y[0]+=x[i]*a[i];
for(k=1;k<n;k++)
{
y[k]=0;
for(j=1;j<n;j++)
x2[j]=a[j-1];
x2[0]=a[n-1];
for(i=0;i<n;i++)
{
a[i]=x2[i];
y[k]+=x[i]*x2[i];
}
}
printf("the circular convolution is\n");
for(i=0;i<n;i++)
printf("%d\t",y[i]);
}

INPUT: (Circular Convolution)


Enter the length of the 1st sequence 5
Enter the length of the second sequence 5
Enter the 1st sequence 1 2 3 4 5
Enter the second sequence 1 2 3 4 5
OUTPUT:
The circular convolution is
45 50 50 45 35
PROGRAM: (FFT Implementation)
#include<stdio.h>
#include<math.h>
#define N 8
#define PI 3.14159
typedef struct
{
float real,imag;
}
complex;
main()
{
int i;
complex w[N];
complex
x[8]={0,0.0,1,0.0,2,0.0,3,0.0,4,0.0,5,0.0,6,0.0,7,0.0}; complex temp1,temp2;
int j,k,upper_leg,lower_leg,leg_diff,index,step;
for(i=0;i<N;i++)
{
w[i].real=cos((2*PI*i)/(N*2.0));
w[i].imag=-sin((2*PI*i)/(N*2.0));
}
leg_diff=N/2;
step=2;
for(i=0;i<3;i++)
{
index=0;
for(j=0;j<leg_diff;j++)
{
for(upper_leg=j;upper_leg<N;upper_leg+=(2*leg_diff))
{
lower_leg=upper_leg+leg_diff;
temp1.real=(x[upper_leg]).real+(x[lower_leg]).real;
temp1.imag=(x[upper_leg]).imag+(x[lower_leg]).imag;
temp2.real=(x[upper_leg]).real-(x[lower_leg]).real;
temp2.imag=(x[upper_leg]).imag-(x[lower_leg]).imag;
(x[lower_leg]).real=temp2.real*(w[index]).real-
temp2.imag*(w[index]).imag;
(x[lower_leg]).imag=temp2.real*(w[index]).imag+temp2.imag
*(w[index]).real;
(x[upper_leg]).real=temp1.real;
(x[upper_leg]).imag=temp1.imag;
}
index+=step;
}
leg_diff=(leg_diff)/2;
step=step*2;
}
j=0;
for(i=1;i<(N-1);i++)
k=N/2;
while(k<=j)
{
j=j-k;
k=k/2;
}
j=j+k;
if(i<j)
{
temp1.real=(x[j]).real;
temp1.imag=(x[j]).imag;
(x[j]).real=(x[i]).real;
(x[j]).imag=(x[i]).imag;
(x[i]).real=temp1.real;
(x[i]).imag=temp1.imag;
}
}
printf("the fft of the given input sequence is \n");
for(i=0;i<8;i++)
{
printf("%f %f \n",(x[i]).real,(x[i]).imag);
}
}

OUTPUT: (FFT Implementation)


The FFT of the given input sequence is:
28.000000 0.000000
-4.000012 9.656858
-4.000005 4.000000
-4.000010 1.656851
-4.000000 0.000000
-3.999998 -1.656858
-3.999995 -4.000000
-3.999980 -9.656851
PROGRAM: (Sine waveform)
#include <stdio.h>
#include <math.h>
float a[500];
void main()
{
int i=0;
for(i=0;i<500;i++)
{
a[i]=sin(2*3.14*10000*i);
}
}
OUTPUT: ( Sine waveform)
PROGRAM: ( Square waveform)
#include <stdio.h>
#include <math.h>
int a[1000];
void main()
{
int i,j=0;
int b=5;
for(i=0;i<10;i++)
{
for (j=0;j<=50;j++)
{
a[(50*i)+j]=b;
}
b=b*(-1) ;
}
}
OUTPUT: ( Square waveform)
PROGRAM: (FIR Filters)
#include<stdio.h>
#include<math.h>
#define pi 3.1415
int n,N,c;
float wr[64],wt[64];
void main()
{
printf("\n enter no. of samples,N= :"); scanf("%d",&N);
printf("\n enter choice of window function\n 1.rect
\n 2. triang \n c= :");
scanf("%d",&c);
printf("\n elements of window function are:"); switch(c)
{
case 1:
for(n=0;n<=N-1;n++)
{
wr[n]=1;
printf(" \n wr[%d]=%f",n,wr[n]);
}
break;
case 2:
for(n=0;n<=N-1;n++)
{
wt[n]=1-(2*(float)n/(N-1));
printf("\n wt[%d]=%f",n,wt[n]);
}
break;
}
}
OUTPUT: (FIR Filters)
PROGRAM: (IIR Filters)
#include<stdio.h>
#include<math.h>
int i,w,wc,c,N;
float H[100];
float mul(float,int);
void main()
{
printf("\n Enter order of filter"); scanf("%d",&N);
printf("\n Enter the cut off frequency"); scanf("%d",&wc);
printf("\n Enter the choice for IIR Filter 1.LPF 2.HPF");
scanf("%d",&c);
switch(c)
{
case 1:
for(w=0;w<100;w++)
{
H[w]=1/sqrt(1+mul((w/(float)wc),2*N));
printf("H[%d]=%f\n",w,H[w]);
}
break;
case 2:
for(w=0;w<=100;w++)
{
H[w]=1/sqrt(1+mul((float)wc/w,2*N));
printf("H[%d]=%f\n",w,H[w]);
}
break;
}
}
float mul(float a,int x)
{
for(i=0;i<x-1;i++)
a*=a;
return(a); }

INPUT: (IIR Filters)


Enter order of filter
2
Enter the cut off frequency
50
Enter the choice for IIR Filter 1.LPF 2.HPF:
OUTPUT: (IIR Filters)
PROGRAM :
function ADCNoiseGain=ADCNoise(b,a,n,FM)
[B,A] = sos2tf([b a]); %form A(z) and B(z)
[h,t] = impz(B,A,n);
ADCNoiseGain = sum(h.^2)/12.0;
fprintf('ADC noise gain is %f\n\n',ADCNoiseGain); if FM~=1
fprintf('ADC noise is %g^2*%g*q^2\n',[FM ADCNoiseGain]); else
fprintf('ADC noise is %g*q^2\n',ADCNoiseGain); end
function CoeffQuantizeErr(b,a,maxbits,ftype,f,Fs)
%COEFFICIENT QUANTIZATION ERROR ANALYSIS
n=256;
for nbits=2:maxbits [B,A]=QuantizeCoeff(b,a,nbits); [B,A] = sos2tf([B A]);
[h,w] = freqz(B,A,n);
amag = abs(h);
amag = amag/max(amag); response
dev(nbits-1,:) = RippleAtten(ftype,f,amag,n,Fs); fprintf('nbits\tband1\t\tband2\t\tband3\n');
fprintf('%d\t%f\t%f\t%f\n',reshape([(2:maxbits)' dev]',maxbits-1,4));
fprintf('\nfrequency response with quantization noise for desired wordlength:\n');
bits=input('wordlength(32 for unquantized coefficients): ');
[B,A] = sos2tf([b a]);
freqz(B,A,n);
hold on;
[B,A] = QuantizeCoeff(b,a,nbits);
[B,A] = sos2tf([B A])
freqz(B,A,n);
title('Frequency Response for Desired Wordlength');
function Stability(b,a,maxbits) format long;
fprintf('\n\nnbits\tstage\tA1\tA2\tradius1\tangle1\tradiu
s2\tangle2\n');
for nbits=2:maxbits
[B,A]=QuantizeCoeff(b,a,nbits);
for i=1:size(b,1)
r1 = sqrt(abs(A(i,3)));
angle1 = 180/pi*acos(A(i,2)/(-2.0*r1)); r2 = sqrt(abs(a(i,3)));
angle2 = 180/pi*acos(a(i,2)/(-2.0*r2)); fprintf('%d\t%d\t%-7.4f\t%-7.4f\t%-7.4f\t%-7.2f\t%-
7.4f\t%-7.2f\n',nbits,i,A(i,2),A(i,3),r1,angle1,r2,angle2);
end
end
format;
function ScaleFactor(b,a,nstep,size,structure)
norm0 = DirectScale(b,a,0,nstep);
norm1 = DirectScale(b,a,1,nstep); norm2 = DirectScale(b,a,2,size); else
norm0 = CanonicScale(b,a,0,nstep);
norm1 = CanonicScale(b,a,1,nstep);
norm2 = CanonicScale(b,a,2,size);
end
disp('L1-norms of the second order sections:'); disp(norm0); disp('L2-norms of the second
order sections:'); disp(norm1); disp('Loo-norms of the second order sections:');disp(norm2);
function s = DirectScale(b,a,iopt,n)
if(iopt>=3)
s = ones(1,size(b,1));
return;
else
A = 1; B = 1;
for i=1:size(b,1) %loop for each stage A = conv(A,a(i,:));
B = conv(B,b(i,:));
s(i) = GetScaleFactor(B,A,iopt,n);
end
end
function s = CanonicScale(b,a,iopt,n)
if(iopt>=3)
s = ones(1,size(b,1));
return;
else
A = 1; B = 1;
for i=1:size(b,1)
A = conv(A,a(i,:));
if i>1
B = conv(B,b(i-1,:));
end
s(i) = GetScaleFactor(B,A,iopt,n); end end
ylabel('error signal')

You might also like