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;

No comments:

Post a Comment