Page Contents
Let us quickly go through Circular Convolution Matlab Code and go through the topic in detail after the article:
We have tried providing multiple codes for Circular Convolution MatLab Code:
Kindly go through the entire article:
Step 1:Â Start
Step 2:Â Read the first sequence
Step 3:Â Read the second sequence
Step 4: Find the length of the first sequence
Step 5: Find the length of the second sequence
Step 6: Perform circular convolution MatLab for both the sequences using inbuilt function
Step 7:Â Plot the axis graph for sequence
Step 8:Â Display the output sequence
Step 9:Â Stop
Matlab Code for Circular ConvolutionÂ
Code 1
clc; close all; clear all; x1=input('Enter the first sequence : '); x2=input('Enter the second sequence : '); N1=length(x1); N2=length(x2); N=max(N1,N2);  if(N2>N1) x4=[x1,zeros(1,N-N1)]; x5=x2; elseif(N2==N1)        x4=x1;        x5=x2; else x4=x1; x5=[x2,zeros(1,N-N2)]; end  x3=zeros(1,N); for m=0:N-1 x3(m+1)=0; for n=0:N-1        j=mod(m-n,N);        x3(m+1)=x3(m+1)+x4(n+1).*x5(j+1); end end  subplot(4,1,1) stem(x1); title('First Input Sequence'); xlabel('Samples'); ylabel('Amplitude'); subplot(4,1,2) stem(x2); title('Second Input Sequence'); xlabel('Samples'); ylabel('Amplitude'); subplot(4,1,3) stem(x3); title('Circular Convolution Using Modulo Operator'); xlabel('Samples'); ylabel('Amplitude');  %In built function y=cconv(x1,x2,N); subplot(4,1,4) stem(y); title('Circular Convolution using Inbuilt Function'); xlabel('Samples'); ylabel('Amplitude');
you can directly execute the code in matlab
I have briefly explained most of the important parts of the code.
Let’s not dive deeply into the details and get a gist of the code.
Code explanation:
First, N1 and N2 we entered 2 sequences.
[matlab]x1=input(‘Enter the first sequence : ‘);x2=input(‘Enter the second sequence : ‘);[/matlab]
Took N as the max length of either N1 or N2
[matlab]N=max(N1,N2);[/matlab]
If length (N2>N1)
Then we will be padding zeros to make the length of both the sequences equal.
[matlab]x4=[x1,zeros(1,N-N1)];[/matlab]
If length (N1=N2) then no changes to the sequence.
[matlab]elseif(N2==N1)x4=x1;
x5=x2;[/matlab]
and if length (N1>N2) then padding the other sequence with zeros.
[matlab]x5=[x2,zeros(1,N-N2)];[/matlab]
Now let’s plot the curve of the input sequences and the output of matlab code for circular convolution
[matlab]subplot(4,1,1) stem(x1);title(‘First Input Sequence’);
xlabel(‘Samples’);
ylabel(‘Amplitude’);
subplot(4,1,2) stem(x2);
title(‘Second Input Sequence’);
xlabel(‘Samples’);
ylabel(‘Amplitude’);
subplot(4,1,3)
stem(x3);
title(‘Circular Convolution Using Modulo Operator’);
xlabel(‘Samples’);
ylabel(‘Amplitude’);[/matlab]
After plotting all the axis of the graph we will be look using the inbuilt circular convolution MatLab function for performing the code.
[matlab]y=cconv(x1,x2,N);[/matlab]
rest all lines after this are to plot the output curve we get after using the inbuilt circular convolution function in MatLab.
[matlab]%In built function y=cconv(x1,x2,N);subplot(4,1,4)
stem(y);
title(‘Circular Convolution using Inbuilt Function’);
xlabel(‘Samples’);
ylabel(‘Amplitude’);[/matlab]
If you are looking for Linear Convolution Matlab code check it out here:
Linear Convolution Program Using Matlab
Code 2:
[matlab]x = input(‘enter a sequence’);
h = input(‘enter another sequence’);
n1=length(x);
n2 = length(h);
n = max(n1,n2);
a=1:n;
x = [x,zeros(1,n-n1)];
h = [h,zeros(1,n-n2)];
y = zeros(1,n);
for i =0:n-1
for j = 0:n-1
k = mod((i-j),n);
y(i+1) = y(i+1) + x(j+1)*h(k+1);
end
end
stem(a,y)
[/matlab]Output:
Code 3:
[matlab] MATLAB codex1=input(‘Enter the sequence x1(n)=’);
x2=input(‘Enter the sequence x2(n)=’);
N=length(x1)+ length(x2)-1;
X1=fft(x1,N);
X2=fft(x2,N);
Y=X1.*X2;
y=ifft(Y);
% y is the linear convolution of x1(n) and x2(n)
disp(‘Linear convolution of x1(n) and x2(n) is:’);
disp(y)
[/matlab]
Circular Convolution without using inbuilt function cconv(x,y,n)
It is used to convolve 2 different discrete Fourier transforms.
It is way to fast for long sequences than linear convolutions
Code 4
[matlab]Enter x(n):
[1 2 2 1]
Enter h(n):
[1 2 3 1]
x(n) is:
1 2 2 1
h(n) is:
1 2 3 1
Y(n) is:
11 9 10 12[/matlab]
Thus results can be achieved Circular convolution without using cconv(x,y,n)
What is Modulo-n circular convolution?
b = [1 1 2 1 2 2 1 1];
c = cconv(a,b); % Circular convolution
cref = conv(a,b); % Linear convolution
dif = norm(c-cref)
[/matlab]
For more examples visit Mathworks
What is Circular Convolution:
Circular convolution also known as cyclic convolution to two functions which are aperiodic in nature occurs when one of them is convolved in the normal way with a periodic summation of other function.
A similar situation can be observed can be expressed in terms of a periodic summation of both functions, if the infinite integration interval is reduced to just one period. This situation arises in Discrete-Time Fourier Transform(DTFT) and is called periodic convolution.
Let us consider x to be a function with a defined periodic summation, xT, where:
If h is any other function for which the convolution xT * h (Multiply) exists, then the convolution xT ∗ h is periodic and equal to:
where to is an arbitrary parameter and hT is a periodic summation of h.
The second integral is called the periodic convolution of functions xT and hT & is normalized by 1/T.
When xT is expressed as the periodic summation of another function, x, the same operation may also be referred to as a circular convolution of functions h and x.
Please Explain The Code
Will post a detailed explanation regarding the code.
Hey… Please explain the code. I’m not able to reason out the operation within the for loops
Will post a detailed explanation regarding the code.