Você está na página 1de 22

ELEMENTARY SIGNALS

1) ELEMENTARY SIGNALS

clc;clear all;close all;


t=-2:2;y=[zeros(1,2),1,zeros(1,2)];
subplot(331);stem(t,y);title('impulse response');
ylabel('amplitude');xlabel('time index');
n=input('enter length,N=');
t=0:n-1;y1=ones(1,n);
subplot(332);stem(t,y1);title('unit step response');
ylabel('amplitude');xlabel('time index');
n1=input('enter length,N=');
t=0:n1;
subplot(333);stem(t,t);title('unit ramp response');
ylabel('amplitude');xlabel('time index');
n2=input('enter length,N=');
t=0:n2;
a=input('enter a=');
y2=exp(a*t);
subplot(334);stem(t,y2);title('exponential');
ylabel('amplitude');xlabel('time index');
t=0:0.001:1;f=2;y=sin(2*pi*f*t);
subplot(335);stem(t,y);title('sinusoidal');
ylabel('amplitude');xlabel('time index');
t=0:0.001:1;f=2;y=cos(2*pi*f*t);
subplot(336);stem(t,y);title('cosine');
ylabel('amplitude');xlabel('time index');
duty = input('Square Wave Duty Cycle(in %) = ');
y = square((2*pi*f*t),duty);

subplot(313);
stem(t,y);
xlabel('Time Index-n');ylabel('Amplitude');
title('Square Wave');
axis([0 1 0 2]);

LINEAR CONVOLUTION

2) LINEAR CONVOLUTION USING BUILT-IN FUNCTION

clc;clear all;
x=input('Enter the input sequence x[n]=');
lx=input('Enter starting time index of x[n]=');
h=input('Enter the impulse response h[n]=');
lh=input('Enter starting time index of h[n]=');
y=conv(x,h);
n=lx+lh:length(y)+lx+lh-1;
stem(n,y);
ylabel('Amplitude');xlabel('Time');
title('Linear Convolution');

3) CONVOLUTION WITHOUT BUILT-IN FUNCTION

clc;clear all;
x1=input('x1[n]=');
x2=input('x2[n]=');
subplot(311);
stem(x1);

title('x1[n]');
subplot(312);
stem(x2);

title('x2[n]');
M=numel(x1);
N=numel(x2);
x1=[zeros(1,N-1) x1 zeros(1,N-1)];
x2=[fliplr(x2) zeros(1,M+N-2)];
for n=1:M+N-1
x3=x1.*x2;
y(n)=sum(x3(N:M+2*N-2));
x2=rtshift(x2);
end
subplot(313);
stem(y);
title('convolued output');

3.1) rtshift

function [x]=rtshift(x)
t=[0 x];
n=numel(x);
x=t(1:n);
return

CIRCULAR CONVOLUTION

4) CIRCULAR CONVOLUTION

clc;clear all;
x=input('x:');
h=input('h:');
N1=length(x);
N2=length(h);
N=max(N1,N2);
x=[x zeros(1,N-N1)];
h=[h zeros(1,N-N2)];
for n=0:N-1
y(n+1)=0;
for i=0:N-1;
j=mod(n-i,N);
y(n+1)=y(n+1)+x(i+1)*h(j+1);
end;
end;
stem(y(1:N));
title('circular convolution');
xlabel('n');ylabel('y(n)');

CORRELATION

5) AUTOCORRELATION

clc;clear all;
N=1024;
f1=1;
fs=200;
n=0:N-1;
x=sin(2*pi*f1*n/fs);
t=[1:N]*(1/fs);
subplot(211);
plot(t,x);
title('sinewave of frequency 1000 Hz');
xlabel('time,[s]');
ylabel('amplitude');
grid;
Rxx=xcorr(x);
subplot(212);
plot(Rxx);
grid;
title('autocorrelation fn');
xlabel('lags');
ylabel('autocorrelation');

6) CROSSCORRELATION

clc;clear all;
N=1024;
f1=1;
fs=200;
n=0:N-1;
x=sin(2*pi*f1*n/fs);
y=x+10*randn(1,N);
subplot(311);
plot(x);
title('pure sinewave');
xlabel('time,[s]');
ylabel('amplitude');
grid;
subplot(312);
plot(y);
title('pure sinewave + noise ');
grid;
Rxy=xcorr(x,y);
subplot(313);
plot(Rxy);
grid;
title('crosscorrelation fn');

DFT AND IDFT

7) DFT

clc;clear all;
x=input('enter the sequence x[n]');
N=input('enter the length of dft n=');
len=length(x);
if N>len
x=[x zeros(1,N-len)];
elseif N<len
x=[x(1:N)];

end
w=exp(-j*2*pi/N);
n=[0:1:(N-1)];
k=[0:1:(N-1)];
nk=n'*k;
W=w.^nk;
X=x*W;
subplot(311),stem(k,abs(X(1:N)));
title('magnitude plot');

xlabel('k');
ylabel('X(k)');
subplot(312),stem(k,angle(X(1:N)));
title('phase plot');
xlabel('k');
ylabel('< X(k)');

8) IDFT

clc;clear all;
X=input('enter the sequence x[n]');
N=input('enter the length of dft n=');
len=length(X);
if N>len
x=[x zeros(1,N-len)];
elseif N<len
x=[x(1:N)];

end
w=exp(j*2*pi/N);
n=[0:1:(N-1)];
k=[0:1:(N-1)];
nk=n'*k;
W=w.^nk;
x=(X*W)/N;
subplot(311),stem(k,abs(x(1:N)));
title('magnitude plot');
xlabel('n');
ylabel('x(n)');

subplot(312),stem(k,angle(X(1:N)));
title('phase plot');
xlabel('n');
ylabel('< x(n)');

BLOCK CONVOLUTION

9) OVERLAP ADD

clc;clear all;
x=input('enter the input sequence x=');
h=input('enter the input sequence h=');
L=4;
M=length(h);
M1=M-1;
N=L+M1;
Nx=length(x);
r=rem(Nx,L);
x=[x zeros(1,L-r)];
h=[h zeros(1, N-M)];
k=floor(Nx/L);
Y=zeros(k+1,N);

z=zeros(1,M1);
for i=0:k
xp=x(i*L+1:i*L+L);
xk=[xp z];
Y(i+1,:)=circonv(xk,h,N);
end;
yp=Y';
[m,n]=size(yp);
for i=L+1:m;
for j=1:n-1
temp1=i-L;
temp2=j+1;
temp3=yp(temp1,temp2)+yp(i,j);
yp(temp1,temp2)=temp3;
end;
end;
z=1;
for j=1:n
for i=1:m
if((i<=L & j<=n-1)|(j==n))
ypnew(z)=yp(i,j);
z=z+1;
end;
end;
end;
y=ypnew(1:M+Nx-1)

10) OVERLAP SAVE

clc;clear all;

x = input('Input sequence x[n] = ');


h = input('Impluse Response h[n] = ');
N = input('Block Length = ');
if (N<length(h))
error('Block length must be at least equal to the duration of impulse response');
end;

Nx = length(x);
M = length(h);
M1 = M-1;
L = N-M1;
x = [zeros(1,M-1) x zeros(1,N-1)];
h = [h zeros(1,N-M)];
K = floor((Nx+M1-1)/L); % K number of blocks
y = zeros(K+1,N);
for k = 0:K
xk = x(k*L+1:k*L+N);
y(k+1,:) = circonv(xk,h,N)
end;

Y = y(:,M:N)';

%Discard first M-1 elements

Y = (Y(:))';
Y = Y(1:Nx+M-1)

10.1) circonv

function [z]=circonv(x1,x2,L)
for k=0:L-1
z(k+1)=0;
for i=0:L-1
j=mod(k-i,L);

z(k+1)=z(k+1)+x1(i+1)*x2(j+1);
end;
end;
return;

FFT AND IFFT

11) FFT

clc;
x = input('Enter x[n] = ');
N = input('What point FFT? N= ');

x = [x zeros(1,N-length(x))];
t = [0:1:N-1];
k = t;
X = fft(x)

subplot(311);stem(t,x);
xlabel('Time Index - n');ylabel('Amplitude');
title('x[n]');

subplot(312);stem(k,abs(X));
xlabel('Index - k');ylabel('Magnitude');
title('Magnitude Plot');

subplot(313);stem(k,angle(X));
xlabel('Index - k');ylabel('Phase Angle');
title('Phase Plot');

12) IFFT

clc;
X = input('Enter X[k] = ');
k = [0:1:length(X)-1];
t = k;
x = ifft(X);

subplot(311);stem(k,abs(X));
xlabel('Index - k');ylabel('Magnitude');
title('Magnitude Plot');

subplot(312);stem(k,angle(X));
xlabel('Index - k');ylabel('Phase Angle');

title('Phase Plot');

subplot(313);stem(t,x);
xlabel('Time Index - n');ylabel('Amplitude');
title('x[n]');

FIR AND IIR

13) FIR LOW PASS FILTER USING HAMMING WINDOW

clc;clear all;
wp=input('Enter pass band edge frequency');
ws=input('Enter the stop band edge frequency');
rp=input('Enter the pass band ripple in db');
as=input('Ente the stop band attenuation in db');
tr_width=ws-wp;
m=ceil(6.6*pi/tr_width)+1;

n=[0:1:(m-1)];
wc=(ws+wp)/2;
hd=ideal_lp(wc,m);
w_ham=(hamming(m));
h=hd'.*w_ham;
[mag,w]=freqz(h,[1]);
db=20*log10(abs(mag));
subplot(221);
stem(n,hd);
title('ideal impulse response');
axis([0 m-1 -0.1 0.3]);
xlabel('n');ylabel('hd(n)');
subplot(222); stem(n,w_ham);
title('hamming window');
axis([0 m-1 0 1.1]);
xlabel('n');ylabel('h(n)');
subplot(223);
stem(n,h);
title('Actual impulse response');
axis([0 m-1 -0.1 0.3]);
ylabel('h(n)');
subplot(224);plot(w/pi,db);
title('magnitude response in db');grid;
axis([0 1 -100 10]);xlabel('freq in pi units');
ylabel('decibels');

13.1) ideal_lp

function [hd]=ideal_lp(wc,M)
if(wc>pi)
error('cut off frequnvy < pi');

return
end
alpha=(M-1)/2;
n=0:1:(M-1);
m=n-alpha+eps
hd=sin(wc*m)./(pi*m);
return

14) IIR LOW PASS BUTTERWORTH FILTER

clear all;
alphap=4;
alphas=20;
fp=input('Enter fp:');
fs=input('Enter fs:');
F=input('Enter sampling frequency:');
omp=2*pi*fp/F;
oms=2*pi*fs/F;

[n,wn]=buttord(omp,oms,alphap,alphas)
[b,a]=butter(n,wn);
w=0:.01:pi;
[h,om]=freqz(b,a,w,'whole');
m=abs(h);
an=angle(h);
title('IIR');
subplot(211);
plot(om/pi,20*log(m));grid;
ylabel('gain in db');
xlabel('normalised frequency');
subplot(212);
plot(om/pi,an);grid;
ylabel('phase in radian');
xlabel('normailsed frequency');

DIT AND DIF

15) DIT

x=input('Enter x(n)=');
M=input('enter radix=');
N=length(x);
x=[x zeros(1,2^M-N)];
[x]=brev(x,M);
w=exp(-2*pi*j/N);

for m=1:M
p=2^(m-1);
t=0:2^(m-1)-1;
k=(N*t)/2^m;
a=1;
for u=1:2^(M-m)
n=a;
A=x(n:n+(2^(m-1)-1));
B=x(n+p:n+p+(2^(m-1)-1));
b=B.*(w.^k);
y(n:n+(2^(m-1)-1))=A+B;
y(n+p:n+p+(2^(m-1)-1))=A-B;
a=n+(2^(m-1)-1)+2^(m-1)+1;
end
x=y;
end
X=x

15.1) brev

function[y]=brev(x,M);
l=M;
a=0:1:((2.^l)-1);
b=dec2bin(a);
d=b(:,end:-1:1);
b=bin2dec(d);
y(a+1)=x(b+1);
end

16) DIF

x=input('Enter x(n)=');
M=input('enter radix=');
N=length(x);
x=[x zeros(1,2^M-N)];
w=exp(-j*2*pi/N);
for m=1:M
p=2^(M-m);
P=2^(M-m+1);
q=2^(m-1);
t=0:2^(M-m)-1;
k=N*t/2^(M-m+1);
for u=0:q-1
n=1+u*P:p+u*P;
A=x(n);
B=x(n+p);
y(n)=A+B;
y(n+p)=(A-B).*(w.^k);
x=y;
end
end
[y]=brev(y,M);
y

Você também pode gostar