Showing posts with label Digital Signal Processing. Show all posts
Showing posts with label Digital Signal Processing. Show all posts

Basic Programs used in Digital Signal Processing using MATLAB

%Unit step%
n=-99:1:100;
u=[zeros(1,100) ones(1,100)];
plot (n,u);
stem (n,u);

%Ramp%
n1=input('enter the limit');
n2=0:n1;
stem(n2,n2)

%real exponential%
n=0:35;
a=1.2;
k=.2;
x=k*a.^+n;
stem (n,x);

%complex exponential%
n=-10:1:10;
alpha=-.1+.3j;
x=exp(alpha*+n);
subplot(2,2,1);stem(n,real(x))
subplot(2,2,2);stem(n,imag(x))
subplot(2,2,1);stem(n,abs(x))
subplot(2,2,1);stem(n,(180/pi)*angle(x))

%sine wave%
t=0:.01:1;
x=sin(2*pi*t);
plot(t,x);
stem(n,x);
Continue Reading

Impulse Response, Step Response, DFT, IDFT, DTFT, IDTFT MATLAB code

%impulse and step response%
sys=tf([1],[1 4 3]);
subplot(2,1,1); step(sys);
ylabel('amplitude');
xlabel('time in seconds');
subplot(2,1,2); impulse(sys);
ylabel('amplitude');
xlabel('time in seconds');

%DFT%
x=input('enter the input matrix: ');
mul=(-2)*j*pi;
xlen=length(x);
for k=0:xlen-1
    y(1,k+1)=0;
    for n=0:xlen-1
        y(1,k+1)=y(1,k+1)+x(1,n+1)*exp(mul*n*k/xlen);
    end
end
nm=1:xlen;
subplot(2,1,1); stem(nm,real(y),'fill')
title('real part');
subplot(2,1,2); stem(nm,imag(y),'fill')
title('imaginary part');

%IDFT%
x=input('enter the input matrix');
mul=(2)*j*pi;
xlen=length(x);
for n=1:xlen-1
    y(1,n+1)=0;
    for k=1:xlen-1
        y(1,n+1)=y(1,n+1)+x(1,k+1)*exp(mul*n*k/xlen);
    end
    y(1,n+1)=y(1,n+1)/xlen;
end
nm=1:xlen;
subplot(2,1,1); stem(nm,real(y),'fill')
title('real part');
subplot(2,1,2); stem(nm,imag(y),'fill')
title('imaginary part');

%DFT and IDFT using matlab functions

x=input('Enter the sequence x(n)=');
N=input('Enter the length of the DFT N=');
X=fft(x)/N;
n=0:length(X)-1;
subplot(311);
stem(n,x);
title('Input Sequence');
subplot(323);
stem(n,X);
title('DFT');
subplot(324);
stem(n,abs(X));
title('Magnitude spectrum');
subplot(325);
stem(n,angle(X));
title('Phase spectrum');
xr=N*ifft(X);
subplot(326);
stem(n,abs(xr));
title('IDFT');

%DTFT and IDTFT%
 N=10;
w=0:0.01*pi:2*pi;
dtft=N.*sinc(w.*N./2./pi)./(sinc(w./2./pi)).*exp(-j.*w.*(N-1)./2);
subplot(2,1,1)
Mag=abs(dtft);
plot(w./pi,Mag);
subplot(2,1,2)
Pha=angle(dtft);
plot(w./pi,Pha);

Continue Reading

DFT without using MATLAB function

%DFT without using MATLAB function%
x=input('Enter the sequence x= ');
N=input('Enter the length of the DFT N= ');
len=length(x);
if N>len
    x=[x zeros(1,N-len)];
elseif N<len
    x=x(1:N);
end
i=sqrt(-1);
w=exp(-i*2*pi/N);
n=0:(N-1);
k=0:(N-1);
nk=n'*k;
W=w.^nk;
X=x*W;
disp(X);
subplot(211);
stem(k,abs(X));
title('Magnitude Spectrum');
xlabel('Discrete frequency');
ylabel('Amplitude');
grid on;
subplot(212);
stem(k,angle(X));
title('Phase Spectrum');
xlabel('Discrete frequency');
ylabel('Phase Angle');
grid on;
Continue Reading

FIR Low Pass Filter using inbuilt MATLAB function

%equiripple FIR filter
Fs=1000;
Fp=input('Input the pass band frequency Fp= ');
Fst=input('Input the stop band frequency Fst= ');
Ap=input('Input the pass band attenuation Ap= ');
Ast=input('Input the stop band attenuation Ast= ');
d=fdesign.lowpass('Fp,Fst,Ap,Ast');
Hd=design(d,'equiripple');
fvtool(Hd);

Continue Reading

Ideal FIR Low Pass Filter without using inbuilt MATLAB function

%ideal low pass linear phase FIR filter%
wc=input('Input the cut-off frequency in radians(less than pi)');
M=input('Input the length of ideal filter');
if wc>pi
    error('cut-off frequency should be less than pi')
    return
end
alpha=(M-1)/2;
n=0:1:(M-1);
m=n-alpha+eps;
hd=sin(wc*m)./(pi*m);
if nargout==0
    stem(n,hd);
    title('Impulse response of ideal low pass filter');
    xlabel('n');
    ylabel('hd(n)');
end


Continue Reading

IDFT without using MATLAB function

%IDFT without using MATLAB function%
X=input('Enter the sequence');
N=input('Enter the length of the IDFT');
len=length(X);
if N>len
    X=[X zeros(1,N-len)];
elseif N<len
    X=X(1:N);
end
i=sqrt(-1);
w=exp(-i*2*pi/N);
n=0:(N-1);
k=0:(N-1);
nk=n'*k;
W=w.^(-nk);
x=(X*W)/N;
disp(x);
subplot(211);
stem(k,abs(x));
title('Magnitude Plot');
xlabel('N');
ylabel('Amplitude');
grid on;
subplot(212);
stem(k,angle(x));
title('Phase Plot');
xlabel('N');
ylabel('Phase Angle');
grid on;

Continue Reading

Linear Circular Convolution using MATLAB code

%Linear convolution%
x=[1 2 3];
y=[1 1 1];
xlen=length(x);
ylen=length(y);
for i=1:xlen
    for j=1:ylen
        z(i,i+j-1)=x(i)*y(j);
    end
end
s=sum(z)
stem(s)

%Circular Convolution using FFT%
x=[1 2 3 4 5];
h=[1 2 1 2 0];
y=fft(x).*fft(h);
y=real(y);
y=ifft(y);
disp(y);


 %auto-correlation%
 n=1024;
 f1=1;
 fs=200;
 n=0:n-1;
 x=sin(2*pi*f1*n/fs);
 t=[1:n]*(1/fs);
 subplot(2,1,1);
 plot(x);
 title('sine wave of freq. 1000hz (fs=8000hz)');
 xlabel('time,[s]')
 ylabel('amplitude')
 grid;
 rxx=xcorr(x);
 subplot(2,1,2); plot(rxx);
 grid;
 title('autocorrelation function of sine wave');
 xlabel('logs');
 ylabel('autocorrelation');

 %crosscorrelation%
 n=1024;
 f=1;
 fs=200;
 n=0:n-1;
 x=sin(2*pi*f*n/fs);
 y=x+(10*randn(1,length(x)));
 subplot(3,1,1); plot(x);
 title('pure sine wave');
 grid;
 subplot(3,1,2); plot(y);
 title('pure sine wave + noise');
 grid;
 rxx=xcorr(x,y);
 subplot(3,1,3); plot(rxx);
 title('cross correlation rxx');
 grid;
Continue Reading