Showing posts with label convolution. Show all posts
Showing posts with label convolution. Show all posts

MATLAB program to perform linear convolution using circular convolution

Program code

%linear convolution using circular convolution
clc;
clear all;
close all;
x=input('Please enter the first sequence x[n] = ');
h=input('Please enter the second sequence h[n] = ');
len_x=length(x);
len_h=length(h);
h=[h zeros(1,len_x-1)];
x=[x zeros(1,len_h-1)];
L=len_x+len_h-1;
for i=0:L-1
    for j=1:L-i;
        a(j+i,i+1)=x(j);
    end
end
for m=1:L-1
    for n=m+1:L
        a(m,n)=x(L-n+m+1);
    end
end
b=transpose(h);
y=a*b;
y=transpose(y);
subplot(311);
stem(x);
disp('The sequence obtained after linear convolution of x[n] with h[n], is given below. ');
disp(y);
grid on;
title('First sequence x[n] ');
subplot(312);
stem(h);
grid on;
title('Second sequence h[n] ');
subplot(313);
stem(y);


Example of output

Please enter the first sequence x[n] = [1 2 3 4 5 6]
Please enter the second sequence h[n] = [1 2 3 4 5 6]
The sequence obtained after linear convolution of x[n] with h[n], is given below.
     1     4    10    20    35    56    70    76    73    60    36

________________________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .


MATLAB program to perform circular convolution of two signals

Program Code

%circular convolution
clc;
close all;
clear all;
x=input('Please enter the first sequence x[n] = ');
h=input('Please enter the second sequence h[n] = ');
L1=length(x);
L2=length(h);
Len=max(L1,L2);
x=[x zeros(1,Len-L1)];
h=[h zeros(1,Len-L2)];
for i=0:(Len-1)
    y(i+1)=0;
    for j=0:(Len-1)
        m=mod(i-j,Len);
        y(i+1)=y(i+1)+(x(j+1)*h(m+1));
    end
end
disp('Convoluted sequence of x[n] and h[n] is given below: ');
disp(y);
subplot(311);
stem(x);
title('First sequence x[n]');
grid on;
subplot(312);
stem(h);
title('Second sequence h[n]');
grid on;
subplot(313);
stem(y);
title('Convoluted sequence ');

grid on;


Example of Output
Please enter the first sequence x[n] = [1 3 2 1]
Please enter the second sequence h[n] = [2 1 2]
Convoluted sequence of x[n] and h[n] is given below: 
     7     9     9    10



________________________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .

MATLAB program to perform linear convolution of two signals ( without using MATLAB function)

Program Code
clc;
close all;
clear all;
xt=input('Please enter the input sequence in time domain x[n]= ');
lxt=length(xt);
ht=input('Please enter the impulse sequence h[n]= ');
lht=length(ht);
ext=[xt zeros(1,(lht-1))];
eht=[ht zeros(1,(lxt-1))];
xdft=fft(ext);
hdft=fft(eht);
freqm=xdft.*hdft;
yt=[ifft(freqm)];
display('The convoluted sequence is given below');
disp(yt);
subplot(311);
stem(xt);
xlabel('Time');
ylabel('Magnitude');
title('Input sequence x[n]');
subplot(312);
stem(xt);
xlabel('Time');
ylabel('Magnitude');
title('Input sequence 1');
subplot(313);
stem(yt);
xlabel('Time');
ylabel('Magnitude');
title('Convoluted sequence');


Example of Output
Please enter the input sequence in time domain x[n]= [1 2 3 4 5 6]
Please enter the impulse sequence h[n]= [1 2 3 4 5 6]
The convoluted sequence is given below
  Columns 1 through 5 

    1.0000    4.0000   10.0000   20.0000   35.0000

  Columns 6 through 10 

   56.0000   70.0000   76.0000   73.0000   60.0000

  Column 11 

   36.0000



Matlab program to find the linear convolution of two signals (using matlab functions)

Program Code
%linear convolution (using matlab functions)
clc;
close all;
clear all;
x1=input('Please enter the input sequence x[n]= ');
x2=input('Please enter the starting time index of x[n]= ');
h1=input('Please enter the impulse response h[n]= ');
h2=input('Please enter the starting time index of h[n]= ');
y=conv(x1,h1);
n=x2+h2:length(y)+x2+h2-1;
display('The convoluted sequence is given below:');
y
stem(n,y);
xlabel('Time');
ylabel('Amplitude');
title('Linear Convolution');



Example of Output
Please enter the input sequence x[n]= [4 3 1 2]
Please enter the starting time index of x[n]= -2
Please enter the impulse response h[n]= [1 4 3]
Please enter the starting time index of h[n]= -1
The convoluted sequence is given below:

y =


     4    19    25    15    11     6

_________________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .