Circular Convolution-1
Circular Convolution-1
function)
% Circular Convolution in MATLAB
clc;
clear;
% Ensure the sequences have the same length by zero-padding the shorter one
N = max(length(x), length(h));
y = zeros(1, N);
for n = 1:N
for k = 1:N
if index < 0
end
y(n) = y(n) + x(k) * h(index + 1); % Add 1 for MATLAB 1-based indexing
end
end
% Display the result
disp(y);
% Plot x[n]
n=0:N-1;
subplot(3, 1, 1);
stem(n, x, 'filled');
xlabel('n');
ylabel('x[n]');
grid on;
% Plot h[n]
subplot(3, 1, 2);
stem(n, h, 'filled');
xlabel('n'); ylabel('h[n]');
grid on;
% Plot y[n]
subplot(3, 1, 3);
stem(n, y, 'filled');
xlabel('n');
ylabel('y[n]');
grid on;
Circular convolution (with built in function)
clc;
clear;
N = input('Enter the length of circular convolution (optional, press Enter for default): ');
N = max(length(x), length(h));
end
y = cconv(x, h, N);
disp(y);
% Plot x[n]
n=0:length(x)-1;
subplot(3, 1, 1);
stem(n, x, 'filled');
xlabel('n'); ylabel('x[n]');
grid on;
% Plot h[n]
n= 0:length(h)-1;
subplot(3, 1, 2);
stem(n, h, 'filled');
xlabel('n'); ylabel('h[n]');
grid on;
% Plot y[n]
n= 0:length(y)-1;
subplot(3, 1, 3);
stem(n, y, 'filled');
xlabel('n');
ylabel('y[n]');
grid on;