Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
57 views

DSP Basicprograms

The document describes methods for generating various signal waves and performing signal processing operations including filtering and frequency domain analysis. It includes code snippets to generate sine, cosine, ramp, step, triangular, square, and sawtooth waves. It also includes code for linear and circular convolution, autocorrelation, cross correlation, the discrete Fourier transform and its inverse, calculating the power spectral density using these functions, and implementing analog and digital Butterworth, Chebyshev, and FIR filters.

Uploaded by

thangam8891
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

DSP Basicprograms

The document describes methods for generating various signal waves and performing signal processing operations including filtering and frequency domain analysis. It includes code snippets to generate sine, cosine, ramp, step, triangular, square, and sawtooth waves. It also includes code for linear and circular convolution, autocorrelation, cross correlation, the discrete Fourier transform and its inverse, calculating the power spectral density using these functions, and implementing analog and digital Butterworth, Chebyshev, and FIR filters.

Uploaded by

thangam8891
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 22

SIGNAL GENERATIONS

Sine wave
clear all;
clc;
x=pi/10:pi/10:10*pi
y=sin(x);
plot(x,y)

Cosine wave
clear all;
clc;
x=pi/10:pi/10:10*pi
y=cos(x);
plot(x,y)

Ramp
clc;
clear all;
y=0:5
plot(y)

Step
clc;
clear all;
y=[0 ones(1,10)]
plot(y)

Exponential wave
clear all;
clc;

t=1:0.1:10
y=cosh(t)-sinh(t);
plot(y);
title('impulse');
xlabel('time');
ylabel('amplitude');

Triangular wave
clc;
clear all;
t=0;
for i=0:2
for x=0:10
t=t+1;
if(x<=5)
y(t)=x;
else
y(t)=10-x;
end
end
end
plot(y);

Square wave
clc;
clear all;
t=0;
for i=0:2
for x=0:10
t=t+1;
if(x<=5)
y(t)=-0.5;
else
y(t)=0.5;
end
end
end
plot(y);

Sawtooth wave
clc;
clear all;
t=0;
for i=0:2
for x=0:10
t=t+1;
if(x<=10)
y(t)=x;
else
y(t)=0;
end
end
end
plot(y);

Linear convolution
clear all;
clc;
x=[1 2 3 4];
h=[1 2 3 4 5]
N1=length(x);
N2=length(h);
X=[x,zeros(1,N2-1)];
H=[k,zeros(1,N1-1)];
for i=1:N1+N2-1
y(i)=0;
for j=1:N1
if(i-j+1>0)
y(i)=y(i)+X(j)*H(i-j+1)
end
end
end
stem(y)

Circular convolution
clear all;
clc;
x=[1 2 2 1];
h=[1 2 3 1];
N=length(x);
x1=fliplr(x);
x1=[x1(N) x1(1:N-1)]
for i=1:N
k=h';
y(i)=x1*k;
x1=[x1(end) x1(1:end-1)];
end
stem(y);

Auto correlation
x=[1 2 1 1];
h=fliplr(x);
N=length(x);
X=[x,zeros(1,N-1)];
H=[h,zeros(1,N-1)];
for i=1:2*N-1
y(i)=0;
for j=1:N
if(i-j+1>0)
y(i)=y(i)+X(j)*H(i-j+1);
end
end
end
stem(y)

Cross correlation
clear all;
clc;
x=[1 2 3 4];
h1=[1 2 3 4 5]
h=fliplr(h1);
N1=length(x);
N2=length(h);
X=[x,zeros(1,N2-1)];
H=[h,zeros(1,N1-1)];
for i=1:N1+N2-1
y(i)=0;
for j=1:N1
if(i-j+1>0)
y(i)=y(i)+X(j)*H(i-j+1)
end
end
end
stem(y)

Discrete fourier transform


clc;
clear all;
z=[1 1 0 0];
n=length(z);
for k=0:n-1
w(k+1)=0;
for i=0:n-1
w(k+1)=w(k+1)+z(i+1)*exp((-j*2*pi*k*i)/n);
end
end
disp('result');w

Inverse Discrete fourier transform


clc;
clear all;
x=[1 1 0 0];
n=length(x);
for m=0:n-1
y(m+1)=0;
for i=0:n-1
y(m+1)=y(m+1)+(x(i+1)*exp((j*2*pi*m*i)/n))/n;
end
end
disp('result');w

PSD using functions


clc;
clear all;
x=[1 1 0 0];
l=16;
z=autocorr(x);
w=dft(z);
x1=(w.*conj(w))/l;
plot(x1);

dft
function w=dft(z)
%z=[1 1 0 0];
n=length(z);
for k=0:n-1
w(k+1)=0;
for i=0:n-1
w(k+1)=w(k+1)+z(i+1)*exp((-j*2*pi*k*i)/n);
end
end
disp('result');w

autocorr
function y=autocorr(x)
%x=[1 2 1 1];
h=fliplr(x);
N=length(x);
X=[x,zeros(1,N-1)];
H=[h,zeros(1,N-1)];
for i=1:2*N-1
y(i)=0;
for j=1:N
if(i-j+1>0)
y(i)=y(i)+X(j)*H(i-j+1);
end
end

end
stem(y)

Analog butterworth(low pass) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter f');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn,'s');
[h,w]=freqs(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(y);
subplot(2,1,2);
plot(angle(h));
Analog butterworth(high pass) filter
clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter f');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn,'high','s');
[h,w]=freqs(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(y);
subplot(2,1,2);

plot(angle(h));

Analog butterworth(band pass) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter f');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn,'bandpass','s');
[h,w]=freqs(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(y);
subplot(2,1,2);
plot(angle(h));
Analog butterworth(band stop) filter
clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter f');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn,'stop','s');
[h,w]=freqs(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(y);
subplot(2,1,2);
plot(angle(h)

Digital butterworth(low pass) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn);
[h,w]=freqz(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(w/pi,y);
subplot(2,1,2);
plot(w/pi,angle(h));
Digital butterworth(high pass) filter
clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn,'high');
[h,w]=freqz(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(w/pi,y);
subplot(2,1,2);
plot(w/pi,angle(h));

Digital butterworth(band pass) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn,'bandpass');
[h,w]=freqz(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(w/pi,y);
subplot(2,1,2);
plot(w/pi,angle(h));

Digital butterworth(band stop) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn,'stop');
[h,w]=freqz(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(w/pi,y);
subplot(2,1,2);
plot(w/pi,angle(h));

Analog Chebyshev type-1 (low pass) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rp,wn,'s');
[h,w]=freqs(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(y);
subplot(2,1,2);
plot(angle(h));
Analog Chebyshev type-1 (high pass) filter
clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rp,wn,'high','s');
[h,w]=freqs(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(y);
subplot(2,1,2);
plot(angle(h));

Analog Chebyshev type-1 (band pass) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rp,wn,'bandpass','s');
[h,w]=freqs(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(y);
subplot(2,1,2);
plot(angle(h));

Analog Chebyshev type-1 (band stop) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rp,wn,'stop','s');
[h,w]=freqs(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(y);
subplot(2,1,2);
plot(angle(h));

Digital Chebyshev type-1(low pass) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rp,wn,);
[h,w]=freqz(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(w/pi,y);
subplot(2,1,2);
plot(w/pi,angle(h));
Digital Chebyshev type-1(high pass) filter
clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rp,wn,'high');
[h,w]=freqz(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(w/pi,y);
subplot(2,1,2);
plot(w/pi,angle(h));

Digital Chebyshev type-1(band pass) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rp,wn,'bandpass');
[h,w]=freqz(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(w/pi,y);
subplot(2,1,2);
plot(w/pi,angle(h));

Digital Chebyshev type-1(bandstop) filter


clear all;
clc;
rp=input('enter rp');
rs=input('enter rs');
wp=input('enter wp');
ws=input('enter ws');
f=input('enter fs');
wp=(2*wp)/f;
ws=(2*ws)/f;
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rp,wn,'stop');
[h,w]=freqz(b,a);
y=20*log10(abs(h));
subplot(2,1,1);
plot(w/pi,y);
subplot(2,1,2);
plot(w/pi,angle(h));

FIR LPF USING RECTANGULAR WINDOW


clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=boxcar(n+1);
b=fir1(n,wp,y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR HPF USING RECTANGULAR WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=boxcar(n+1);
b=fir1(n,wp,'high',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR BPF USING RECTANGULAR WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=boxcar(n+1);
b=fir1(n,wp,'bandpass',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);

FIR BRF USING RECTANGULAR WINDOW


clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=boxcar(n+1);
b=fir1(n,wp,'stop',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR LPF USING BARTLETT WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=bartlett(n+1);
b=fir1(n,wp,y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR HPF USING BARTLETT WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=bartlett(n+1);
b=fir1(n,wp,'high',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);

FIR BPF USING BARTLETT WINDOW


clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=bartlett(n+1);
b=fir1(n,wp,'bandpass',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR BRF USING BARTLETT WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=bartlett(n+1);
b=fir1(n,wp,'stop',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);

FIR LPF USING KAISERWINDOW


clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
p=input('enter beta value');
n=input('enter n');
wp=(2*wp)/fs;
y=kaiser(n+1,p);
b=fir1(n,wp,y);
[h,w]=freqz(b,1,256);

m=20*log10(abs(h));
plot(w/pi,m);
FIR HPF USING KAISERWINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
p=input('enter beta value');
n=input('enter n');
wp=(2*wp)/fs;
y=kaiser(n+1,p);
b=fir1(n,wp,'high',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR BPF USING KAISERWINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
p=input('enter beta value');
n=input('enter n');
wp=(2*wp)/fs;
y=kaiser(n+1,p);
b=fir1(n,wp,'bandpass',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR BRF USING KAISERWINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
p=input('enter beta value');
n=input('enter n');
wp=(2*wp)/fs;
y=kaiser(n+1,p);
b=fir1(n,wp,'stop',y);
[h,w]=freqz(b,1,256);

m=20*log10(abs(h));
plot(w/pi,m);
FIR LPF USING HAMMING WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=hamming(n+1);
b=fir1(n,wp,y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR HPF USING HAMMING WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=hamming(n+1);
b=fir1(n,wp,'high',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR BPF USING HAMMING WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=hamming(n+1);
b=fir1(n,wp,'bandpass',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));

plot(w/pi,m);
FIR BRF USING HAMMING WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=hamming(n+1);
b=fir1(n,wp,'stop',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);

FIR LPF USING HANNING WINDOW


clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=hanning(n+1);
b=fir1(n,wp,y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR HPF USING HANNING WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=hanning(n+1);
b=fir1(n,wp,'high',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));

plot(w/pi,m);

FIR BPF USING HANNING WINDOW


clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=hanning(n+1);
b=fir1(n,wp,'bandpass',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR BRF USING HANNING WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=hanning(n+1);
b=fir1(n,wp,'stop',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR LPF USING BLACKMAN WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=blackman(n+1);
b=fir1(n,wp,'stop',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));

plot(w/pi,m);
FIR HPF USING BLACKMAN WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=blackman(n+1);
b=fir1(n,wp,'stop',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR BPF USING BLACKMAN WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=blackman(n+1);
b=fir1(n,wp,'stop',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);
FIR BRF USING BLACKMAN WINDOW
clear all;
clc;
wp=input('enter wp');
fs=input('enter fs');
n=input('enter n');
wp=(2*wp)/fs;
y=blackman(n+1);
b=fir1(n,wp,'stop',y);
[h,w]=freqz(b,1,256);
m=20*log10(abs(h));
plot(w/pi,m);

You might also like