Showing posts with label DSP MATLAB PROGRAMS. Show all posts
Showing posts with label DSP MATLAB PROGRAMS. Show all posts

Friday, August 21, 2009

CIRCULAR CONVOLUTION MATLAB PROGRAM

CIRCULAR CONVOLUTION


 

function f=circonv(a,b)

a=input('enter the first sequence=')

b=input('enter the second sequence=')

N1=length(a)

N2=length(b)

N=max(N1,N2)

a=[a zeros(1,N-N1)]

b=[b zeros(1,N-N2)]

for n=0:N-1

f(n+1)=0

for i=0:N-1

j=mod(n-i,N)

f(n+1)=f(n+1)+a(i+1)*b(j+1)

end

end

subplot(2,2,1)

stem(a)

xlabel('time index')

ylabel('amplitude')

subplot(2,2,2)

stem(b)

xlabel('time index')

ylabel('amplitude')

subplot(2,1,2)

stem(f)

xlabel('time index')

ylabel('amplitude')

title('circular convolution of two sequence')


 


 


 


 

OBSERVATION:


 

>> circonv(a,b)

enter the first sequence=[1,2,3]

a = 1 2 3

enter the second sequence=[1,2,3,4]

b = 1 2 3 4

N1 = 3

N2 = 4

N = 4

a = 1 2 3 0

b = 1 2 3 4

f = 0

j = 0

f = 1

j = 3

f = 9

j = 2

f = 18

j = 1

f = 18

f = 18 0

j = 1

f = 18 2

j = 0

f = 18 4

j = 3

f = 18 16

j = 2

f = 18 16

f = 18 16 0

j = 2

f = 18 16 3

j = 1

f = 18 16 7

j = 0

f = 18 16 10

j = 3

f = 18 16 10

f = 18 16 10 0

j = 3

f = 18 16 10 4

j = 2

f = 18 16 10 10

j = 1

f = 18 16 10 16

j = 0

f = 18 16 10 16

ans = 18 16 10 16

GENERATION OF FM SIGNAL DSP MATLAB PROGRAM

GENERATION OF FM SIGNAL


 


 

Fc=input('Enter the carrier frequency in Hz, Fc=');

Fm=input('Enter the modulating frequency in Hz, Fm=');

mf=input('Enter the modulation index, m=');

t=0:0.0001:1;

M=sin(2*pi*Fm*t);

Y=sin((2*pi*Fc*t)-(mf*M));

subplot(3,1,1);

plot(t,M);

axis([0 1 -1.5 1.5]);

title('Frequency modulation');

xlabel('Time');

ylabel('Modulation signal');

subplot(3,1,2);

plot(t,C);

axis([0 1 -1.5 1.5]);

xlabel('Time');

ylabel('Carrier signal');

subplot(3,1,3);

plot(t,Y);

axis([0 1 -1.5 1.5]);

xlabel('Time');

ylabel('FM signal');


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 

Observation:


 

Enter the carrier frequency in Hz, Fc=50

Enter the modulating frequency in Hz, Fm=10

Enter the modulation index, mf=2


 


 

FIR FILTER USING DIFFERENT WINDOW MATLAB PROGRAM

FIR FILTER USING DIFFERENT WINDOW


 


 

f=input('Samplin rate in Hz, f=');

fp=input('pass band edge frequency in Hz=');

fs=input('stop band edge frequency in Hz=');

rp=input('pass band ripple in dB=');

rs=input('minimum stop band attenuation in dB=');

wp=2*fp/f;

ws=2*fs/f;

[N,wn]=cheb1ord(wp,ws,rp,rs);


 

%Hann window


 

Hw=hann(N+1);

B=fir1(N,wn,Hw);

[H,omega]=freqz(B,1,256);

gain=20*log(abs(H));

subplot(2,2,1);

plot(omega/pi,gain);

grid;

xlabel('omega/pi');

ylabel('Gain in dB');

title('FIR LPF using HANN window');


 

%Hamming window


 

Hw=hamming(N+1);

B=fir1(N,wn,Hw);

[H,omega]=freqz(B,1,256);

gain=20*log(abs(H));

subplot(2,2,2);

plot(omega/pi,gain);

grid;

xlabel('omega/pi');

ylabel('Gain in dB');

title('FIR LPF using HAMMING window');


 

%Rectangular window


 

Hw=rectwin(N+1);

B=fir1(N,wn,Hw);

[H,omega]=freqz(B,1,256);

gain=20*log(abs(H));

subplot(2,2,3);

plot(omega/pi,gain);

grid;

xlabel('omega/pi');

ylabel('Gain in dB');

title('FIR LPF using RECTANGULAR window');


 

%Triangular window


 

Hw=triang(N+1);

B=fir1(N,wn,Hw);

[H,omega]=freqz(B,1,256);

gain=20*log(abs(H));

subplot(2,2,4);

plot(omega/pi,gain);

grid;

xlabel('omega/pi');

ylabel('Gain in dB');

title('FIR LPF using TRIANGULAR window');


 


 


 


 


 


 


 


 

Observation:


 

Samplin rate in Hz,f=2000

pass band edge frequency in Hz=200

stop band edge frequency in Hz=300

pass band ripple in dB=6

minimum stop band attenuation in dB=30


 


 


 


 

MOVING AVERAGE FILTER MATLAB PROGRAMS

MOVING AVERAGE FILTER


 


 

t=0:.01:1;

f=5;

y=sin(2*pi*f*t);

%Generation of random signal

g=0.5*randn(size(t));

z=g+y;

N=10; %order required

b=1/N*(ones(1,N));

x=filter(b,1,z); %filters noice

subplot(3,1,1);

plot(t,y);

ylabel('pure signal');

subplot(3,1,2);

plot(t,z);

ylabel('noise buried');

subplot(3,1,3);

plot(t,x);

ylabel('filtered signal');

xlabel('Time in seconds');


 


 


 


 


 


 


 


 


 


 


 

CHEBYSHEV TYPE 1 BAND PASS FILTER MATLAB PROGRAM

CHEBYSHEV TYPE 1 BAND PASS FILTER


 


 

alphap=input('pass band attenuation in dB=');

alphas=input('stop band attenuation in dB=');

fp1=input('pass band frequency fp1 in Hz=');

fp2=input('pass band frequency fp2 in Hz=');

fs1=input('stop band frequency fs1 in Hz=');

fs2=input('stop band frequency fs2 in Hz=');

f=input('Sampling frequency in Hz=');

wp1=2*fp1/f;ws1=2*fs1/f;

wp2=2*fp2/f;ws2=2*fs2/f;

wp=[wp1,wp2];

ws=[ws1,ws2];

%To find cutoff frequency and order of the filter

[n,wn]=cheb1ord(wp,ws,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*log(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');


 


 


 


 

Observation:


 

pass band attenuation in dB=2

stop band attenuation in dB=20

pass band frequency fp1 in Hz=100

pass band frequency fp2 in Hz=500

stop band frequency fs1 in Hz=200

stop band frequency fs2 in Hz=400

Sampling frequency in Hz=2000


 


 

CHEBYSHEV TYPE 1 LOW PASS FILTER MATLAB PROGRAM

CHEBYSHEV TYPE 1 LOW PASS FILTER


 


 

alphap=input('pass band attenuation in dB=');

alphas=input('stop band attenuation in dB=');

fp=input('pass band frequency in Hz=');

fs=input('stop band frequency in Hz=');

f=input('Sampling frequency in Hz=');

wp=2*fp/f;ws=2*fs/f;

%To find cutoff frequency and order of the filter

[n,wn]=cheb1ord(wp,ws,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*log(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');


 


 


 


 


 


 


 


 


 


 

Observation:


 

pass band attenuation in dB=1

stop band attenuation in dB=30

pass band frequency in Hz=200

stop band frequency in Hz=600

Sampling frequency in Hz=2000


 


 


 


 

BUTTERWORTH BAND REJECT FILTER MATLAB PROGRAM

BUTTERWORTH BAND REJECT FILTER


 


 

alphap=2;    %Passband attenuation in dB

alphas=20;    %stopband attenuation in dB

ws=[.2,.4];    %stopband frequency in radians

wp=[.1,.5];    %Passband frequency in radians

%To find cutoff frequency and order of the filter

[n,wn]=buttord(wp,ws,alphap,alphas);

%system function of filter

[b,a]=butter(n,wn,'stop');

w=0:.01:pi;

[h,ph]=freqz(b,a,w);

m=20*log(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');


 


 


 

BUTTERWORTH HIGH PASS FILTER MATLAB PROGRAM

BUTTERWORTH HIGH PASS FILTER


 


 

alphap=.4;    %Passband attenuation in db

alphas=30;    %stopband attenuation in db

fs=400;    %stopband frequency in hz

fp=800;    %passband frequency in hz

F=2000;

omp=2*fp/F;oms=2*fs/F;

%To find cutoff frequency and order of the filter

[n,wn]=buttord(omp,oms,alphap,alphas);

%system function of the filter

[b,a]=butter(n,wn,'high');

w=0:.01:pi;

[h,om]=freqz(b,a,w);

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

an=angle(h);

subplot(2,1,1);

plot(om/pi,m);

grid;

ylabel('Gain in db');

xlabel('Normalised frequency');

subplot(2,1,2);

plot(om/pi,an);

grid;

ylabel('Phase in radians');

xlabel('Normalised frequency');

BUTTERWORTH BAND PASS FILTER MATLAB PROGRAM

BUTTERWORTH BAND PASS FILTER


 


 

alphap=2;    %Passband attenuation in dB

alphas=20;    %stopband attenuatio in dB

wp=[.2*pi,.4*pi];    %Passband frequency in radians

ws=[.1*pi,.5*pi];    %Stopband frequency in radians

%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]=butter(n,wn);

w=0:.01:pi;

[h,ph]=freqz(b,a,w);

m=20*log(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');


 

BUTTERWORTH LOW PASS FILTER MATLAB PROGRAM


 

BUTTERWORTH LOW PASS FILTER


 


 

alphap=4;    %Passband attenuation in db

alphas=30;    %Stopband attenuation in db

fp=400;    %Passband frequency in hz

fs=800;    %Stopband frequency in hz

F=2000;    %Sampling frequency in hz

omp=2*fp/F;oms=2*fs/F;

%To find cutoff frequency and order of the filter

[n,wn]=buttord(omp,oms,alphap,alphas);

%system function of the filter

[b,a]=butter(n,wn);

w=0:.01:pi;

[h,om]=freqz(b,a,w,'whole');

m=abs(h);

an=angle(h);

subplot(2,1,1);

plot(om/pi,20*log(m));    

grid;

ylabel('Gain in db');

xlabel('Normalised frequency');

subplot(2,1,2);

plot(om/pi,an);

grid;

ylabel('phase in redians');

xlabel('Normalised frequency');

Dsp matlab program to Generate AM signal

GENERATION OF AM SIGNAL


 


 

Fc=input('Enter the carrier frequency in Hz, Fc=');

Fm=input('Enter the modulating frequency in Hz, Fm=');

m=input('Enter the modulation index, m=');

t=0:0.0001:1;

C=sin(2*pi*Fc*t);

M=sin(2*pi*Fm*t);

Y=(1+m*M).*C;

subplot(3,1,1);

plot(t,M);

axis([0 1 -1.5 1.5]);

title('Amplitude modulation');

xlabel('Time');

ylabel('Modulation signal');

subplot(3,1,2);

plot(t,C);

axis([0 1 -1.5 1.5]);

xlabel('Time');

ylabel('Carrier signal');

subplot(3,1,3);

plot(t,Y);

axis([0 1 -1.5 1.5]);

xlabel('Time');

ylabel('AM signal');


 


 


 

Observation:


 

Enter the carrier frequency in Hz, Fc=50

Enter the modulating frequency in Hz, Fm=5

Enter the modulation index, m=0.6