dsp matlab programs

20
DSP MATLAB programs MATLAB Programs as per Exercises TUESDAY, AUGUST 23, 2011 FFT SPEECH SIGNAL clc; clear; close all fs=20000 t=0:1/fs:.04; x=4*sin(2*pi*2000*t)+4*sin(2*pi*1500*t)+10*sin(2*pi*6000*t)+6*sin(2*pi*200*t); y=fft(x); l=length(y); Y=y(1:(l-1)/2); r=1/((l-1)/2); n=0:r:1-r; plot(n,abs(Y)), xlabel('Normalized frq- 0 to corresponding for sampling rate 20KHz & 1rad=10KHz') % speech_dft.wav % toilet.wav %% ACTUAL PROGRAM%% clc clear

Upload: somendra-sahu

Post on 09-Sep-2014

178 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Dsp Matlab Programs

DSP MATLAB programsMATLAB Programs as per Exercises

TUESDAY, AUGUST 23, 2011

FFT SPEECH SIGNALclc;

clear;

close all

fs=20000

t=0:1/fs:.04;

x=4*sin(2*pi*2000*t)+4*sin(2*pi*1500*t)+10*sin(2*pi*6000*t)+6*sin(2*pi*200*t);

y=fft(x);

l=length(y);

Y=y(1:(l-1)/2);

r=1/((l-1)/2);

n=0:r:1-r;

plot(n,abs(Y)), xlabel('Normalized frq- 0 to corresponding for sampling rate 20KHz & 1rad=10KHz')

% speech_dft.wav

% toilet.wav

%% ACTUAL PROGRAM%%

clc

clear

[Y,FS,NBITS]=wavread('speech_dft.wav');

x=fft(Y);

l=length(x);

Page 2: Dsp Matlab Programs

X=x(1:(l-1)/2);

r=1/((l-1)/2);

n=0:r:1-r;

subplot 211, plot(Y); subplot 212, plot(n,abs(X))

pause

% ½ of FS Hz corresponds to normalized 1 rad

%To remove 200Hz to 300Hz freq range % 2KHz is (2/FS)*200 rad % & 3KHz is (2/FS)*300 rad

rad_start=(2/FS)*200;

rad_stop=(2/FS)*300;

p_start=0;

for j=1:length(n)

if rad_start>=n(j)

p_start=j;

end

end

p_stop=0;

for j=1:length(n)

if rad_stop>=n(j)

p_stop=j;

end

end

Z=x;

for j=p_start:p_stop

Z(j)=0;

end

Page 3: Dsp Matlab Programs

for j=l-p_stop:l-p_start

Z(j)=0;

end

subplot 211, plot(abs(x)); subplot 212, plot(abs(Z))

pause

y=ifft(Z);

subplot 211, plot(abs(Y));subplot 212, plot(abs(y))

wavwrite(y,FS,16,'processed_signal.wav');

wavwrite(y,FS,16,'original_signal.wav');

Posted by Swanirbhar at 11:36 AM 0 comments Links to this post

MONDAY, AUGUST 22, 2011

2N point DFT of real Sequence using a single N-point DFTclcclearv=[1 2 2 2 0 1 1 1];l=length(v);for k=1:l/2;     h(k)=v(2*k);    g(k)=v(2*(k-1)+1);endx=g+i*h;X=fft(x);Xc=conj(X);Xci=[Xc(1) fliplr(Xc(2:4))];

Page 4: Dsp Matlab Programs

G=(1/2)*(X+Xci);H=(-i/2)*(X-Xci);Wn=exp(-i*2*pi/l);for p=1:l    if p>l/2        k=p-l/2;    else        k=p;    end    V(p)=G(k)+((Wn^(p-1))*H(k));enderror=V-fft(v);stem(error)

Posted by SMD at 7:42 PM 0 comments Links to this post

TUESDAY, AUGUST 16, 2011

EC 6120 and EC 5102 DSP MATLAB PROGRAMS DAY 5PROGRAMS FOR FILTERING LONG DATA SEQUENCES

%Program for MATLAB 7 users where circular convolution function is not in built%if present the present program (a function) to b saved as ECEE.mcan be replaced by 'cconv' command

Page 5: Dsp Matlab Programs

function [c]=ECEE(a,b,N)

% a=[1 2 3 4];%input

% b=[1 2 -1 -2];%input

% N=6;%number of points

A=fft(a,N);

B=fft(b,N);

C=A.*B;

c=ifft(C,N);

OVERLAP SAVE METHOD

%Filtering long data sequence using OVERLAP SAVE METHOD

clc

clear all

close all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

x=[1 2 -1 2 3 -2 -3 -1 1 1 2 -1];%input

h=[1 2 1 1];%system

N=4;%length of each block

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

yy=conv(x,h);

subplot 311, stem(yy)

title('Using LONG linear filtering')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if (N

    error('N must be >=length(h)')

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);%no of blocks

y=zeros(K+1,N);

Page 6: Dsp Matlab Programs

%dividing sequence in k blocks

for k=0:K

    xk=x(k*L+1:k*L+N);

    Y(k+1,:)=ECEE(xk,h,N);

end

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

y=(Y(:))';

subplot 312, stem(y)

title('Using Overlap SAVE method')

d=yy-y(1:length(yy));

subplot 313, stem(d)

title('Difference')

OVERLAP ADD METHOD

%Filtering long data sequence using OVERLAP ADD METHOD

clc

clear all

close all

x=[1 2 -1 2 3 -2 -3 -1 1 1 2 -1];%input

h=[1 2 1 1];%system

L=4;%length of each block

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

yy=conv(x,h);

subplot 311, stem(yy)

title('Using LONG linear filtering')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Nx=length(x);

M=length(h);

M1=M-1;

R=rem(Nx,L);

N=L+M1;

x=[x zeros(1,L-R)];

h=[h zeros(1,N-M)];

K=floor(Nx/L); % number of blocks

y=zeros(K+1,N);

Page 7: Dsp Matlab Programs

z=zeros(1,M1);

%dividing to K blocks

for k=0:K

    xp=x(L*k+1:L*k+L);

    xk=[xp z];

    y(k+1,:)=ECEE(xk,h,N);

end

yp=y';

[x,y]=size(yp);

for i=L+1:x;

    for j=1:y-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:y

    for i=1:x

        if ((i<=L & j<=y-1)|(j==y))

            ypnew(z)=yp(i,j);

            z=z+1;

        end

    end

end

y=ypnew;

subplot 312, stem(y)

title('Using Overlap Add method')

d=yy-y(1:length(yy));

subplot 313, stem(d)

title('Difference')

Page 8: Dsp Matlab Programs

Posted by SMD at 3:44 AM 0 comments Links to this post

MONDAY, AUGUST 8 , 2011

EC 6120 and EC 5102 DSP MATLAB PROGRAMS DAY 4AUTO AND CROSS- CORRELATION OF SIGNAL, NOISE AND NOISY SIGNAL

clear all; clc; close all

%Auto correlation and cross correlation fumctions

N = 96;

n = 1 : N;

x = cos(0.25 *pi *n);

rx = conv(x,fliplr(x));

disp('ACF of X=')

min(min(corrcoef(x,x)))

k = -28 : 28;

subplot(3,1,1);

stem(k,rx(68 : 124));

xlabel(' log index');

ylabel(' Amplitude');

title(' clean signal ACF');

Page 9: Dsp Matlab Programs

w=rand(1,N)-0.5;

y=x+w;

ry=conv(y,fliplr(y));

disp('ACF of Y=')

min(min(corrcoef(y,y)))

subplot(3,1,2);

stem (k,ry(68 : 124));

xlabel(' log index');

ylabel(' Amplitude');

title (' clean signal ACF');

rw=conv(w,fliplr(w));

disp('ACF of W=')

min(min(corrcoef(w,w)))

subplot(3,1,3);

stem(k,rw(68 : 124));

xlabel(' log index');

ylabel(' Amplitude');

title(' clean signal ACF');

rxw=conv(x,fliplr(w));

disp('CCF of X with W=')

min(min(corrcoef(x,w)))

figure

subplot (3,1,1);

stem (k,rxw(68 : 124));

xlabel(' log index');

ylabel(' Amplitude');

Page 10: Dsp Matlab Programs

title (' X with W');

rxy=conv(x,fliplr(y));

disp('CCF of X with Y=')

min(min(corrcoef(x,y)))

subplot (3,1,2);

stem (k,rxy(68 : 124));

xlabel(' log index');

ylabel(' Amplitude');

title (' X with Y');

ryw=conv(y,fliplr(w));

disp('ACF of Y with W=')

min(min(corrcoef(y,w)))

subplot (3,1,3);

stem (k,rxy(68 : 124));

xlabel(' log index');

ylabel(' Amplitude');

title (' Y with W');

LTI SYSTEM PLOT USING ZERO-POLE-GAIN TRANSFER FUNCTION

clear all; clc; close all

% for IIR butterworth LPF of order 3 and .5 cut off

[z,p,k] = butter(3,.5);

h=zpk(z,p,k); tf(h)

[num,den] = tfdata(h,'v')

% [b,a] = butter(3,.5)

[h,w]=freqz(num,den,2^8);% considering 8 bits

plot(w/(2*pi),20*log10(h))

Page 11: Dsp Matlab Programs

title('LTI response plot')

figure

subplot(2,1,1), plot(abs(h))

title('magnitude')

axis tight

subplot(2,1,2), plot(w*360/(2*pi))

title('Phase converted to degree')

axis tight

DISCRETE FOURIER TRANSFORM

clear all; clc; close all

%Simple DFT without using the FFT algo of MATLAB

N = 256;

n = 1: N;

xn = cos(0.2 *pi *n);

figure(1);

plot(n,xn);

wn = exp(-j* pi/N);

g= 1;

for k = 1: N,

n= 1:N;

xk(g,1) =xn * (wn * ones(N,1)).^(n' * k);

g = g +1;

end

X1 = abs(xk);

figure(2);

plot(n/(2*N),X1/max(X1));

Page 12: Dsp Matlab Programs

grid;

axis([0,0.5,0,1]);

title('DFT');

xlabel('Frequency in pi unit');

ylabel('Mag');

COMPARISON OF THE dft USING THE EQUATION AND USING MATLAB’S IN BUILT FFT BASED DFT

clear all; clc; close all

%comparing DFT with the in built FFT of MATLAB

N = 256;

n = 1 : N;

xn = cos(0.2 *pi *n);

figure(1);

plot(n,xn);

M=N;

m=0:(M-1);

y = fft(xn,M);

magy=abs(y);

wn = exp(-j* pi/N);

g= 1;

for k = 1: N,

n= 1:N;

xk(g,1) =xn*(wn*ones(N,1)).^(n'*k);

g = g +1;

end

X1 = abs(xk);

Page 13: Dsp Matlab Programs

figure(2);

subplot(2,1,1);

plot(n/(2*N),X1/max(X1));

grid;

axis([0,0.5,0,1]);

title('DFT');

xlabel('Frequency in pi unit');

ylabel('Mag');

subplot(2,1,2);

plot(m/M,magy/max(magy));

grid;

axis([0,0.5,0,1]);

title('DFT with fft function');

xlabel('Frequency in pi unit');

ylabel('Mag');

COMPARISON OF MULTIPLICATION IN FREQUENCY DOMAIN WITH CONVOLUTION IN TIME DOMAIN

clear all; clc; close all

%showing multiplication in frequency is convolutionin time

x=[1 2 3 4];%1st sequence

h=[1 2 1 2];%2nd sequence

l=length(x)+length(h)-1;%length of convolution o/p

xe=fft(x,l);%fft of 1st squence using zero padding

he=fft(h,l);%fft of 2nd squence using zero padding

y1=ifft(xe.*he);%ifft of the prodct of two sequences

y2=conv(x,h);% normal covoluton o/p

Page 14: Dsp Matlab Programs

error=y1-y2;%error between the two convolutions

k=0:l-1;

subplot(3,1,1),stem(k,y1);

subplot(3,1,2),stem(k,y2);

subplot(3,1,3),stem(k,abs(error));

Posted by Swanirbhar at 8:14 AM 0 comments Links to this post

EC 6120 and EC 5102 DSP MATLAB PROGRAMS DAY 3STABILITY CHECK OF AN LTI SYSTEM WITH IF-ELSE FOR INF=10 6

clear all; clc; close all

num=[1 0.8];

den=[1 1.5 .9];

%den=[1 1.5 0.9];% stable

%den=[1 1.5 9];% unstable

N=200;

h=impz(num,den,N+1);

sum=0;

n=0:N;

for k=1:N+1

sum=sum+h(k);

if abs(sum)>10^6;

disp('UNSTABLE LTI SYSTEM');

break

end

Page 15: Dsp Matlab Programs

if abs(h(k))<10^(-6);

disp('STABLE LTI SYSTEM');

break

end

if k==N+1;

disp('STABLE LTI SYSTEM');

end

end

stem(n,h); grid;

disp('Total Sum of impulses ='),

disp(sum)

LINEARITY PROPERY OF A LTI SYSTEM

clear all; clc; close all

%Linearity property of 2 sequences

n=0:40; a=2; b=-3;

x1=cos(2*pi*0.1*n);

x2=cos(2*pi*0.4*n);

x=a*x1+b*x2;

ic=[0 0];% initially relaxed

%ic=[0 2];% initially not relaxed

num=[2.2403 2.4908 2.2403];

den=[1 -0.4 0.75];

y1=filter(num,den,x1,ic);

y2=filter(num,den,x2,ic);

y=filter(num,den,x,ic);

yt=a*y1+b*y2;

Page 16: Dsp Matlab Programs

d=y-yt;

if abs(sum(sum(d)))<10^(-3)

disp('LTI SYSTEM IS LINEAR')

else

disp('LTI SYSTEM IS NON-LINEAR')

end

subplot(3,1,1), stem(n,y); grid

subplot(3,1,2), stem(n,yt); grid

subplot(3,1,3), stem(n,d); grid

TIME SHIFT-INVARIANCE PROPERY OF A LTI SYSTEM

clear all; clc; close all

%Linearity property of 2 sequences

n=0:40;D=10;

x=3*cos(2*pi*0.1*n)-2*cos(2*pi*0.4*n);

xd=[zeros(1,D) x];

num=[2.2403 2.4908 2.2403];

den=[1 -0.4 0.75];

ic=[0 0];% initially relaxed

%ic=[0 2];% initially not relaxed

y=filter(num,den,x,ic);

yd=filter(num,den,xd,ic);

d=y-yd(1+D:41+D);

subplot(3,1,1),stem(y),grid;

subplot(3,1,2),stem(yd),grid;

subplot(3,1,3),stem(d),grid;

if abs(sum(sum(d)))<10^(-3)

Page 17: Dsp Matlab Programs

disp('LTI SYSTEM IS TIME-SHIFT INVARIANT')

else

disp('LTI SYSTEM IS TIME-SHIFT VARIANT')

end

CONVOLUTION PROCESS CLARIFICATION USING FOR LOOP AND IF–ELSE CASES

clear all; clc; close all

% comparison of convolution without command with convolution using the

% 'conv' command to clarify the convolution process

x=[1 4 2 4 1 1];

h=[1 2 3 4 5];

len1=length(x);

len2=length(h);

len=len1+len2-1;

a=fliplr(h);

for i=1:len

c(i)=0;

for j=1:len1

if j>i

continue;

end

if(len2-i+j)<=0

continue;

end

c(i)=c(i)+(x(j)*a(len2-i+j));

end

end

Page 18: Dsp Matlab Programs

k=1:len;

conv_op1=c(1:len)

subplot(2,1,1),stem(k,conv_op1);

title('Without using "conv" command')

conv_op2=conv(x,h)% uses the filter command internally

subplot(2,1,2),stem(k,conv_op2)

Posted by Swanirbhar at 8:13 AM 0 comments Links to this post

Older Posts Home

Subscribe to: Posts (Atom)My Links

My Profile Departmental Profile ACADEMIC SECTION Blog Journal Crap Programs

Blog Archive

      

Contributors Swanirbhar SMD

Page 19: Dsp Matlab Programs

Search This Blog