%% Circular Convolution %% clc; clear all; close all; % Given sequences x = [1 -1 -2 3 -1]; h = [1 2 3]; n1 = length(x); n2 = length(h); N = max(n1, n2); % Zero-padding x = [x zeros(1, N - n1)]; h = [h zeros(1, N - n2)]; %% Method-1: Using Circular Convolution Formula y1 = zeros(1, N); for n = 0:N-1 for i = 0:N-1 j = mod(n - i, N); y1(n+1) = y1(n+1) + x(i+1) * h(j+1); end end disp('Output by Method-1 (Formula):'); disp(y1); %% Method-2: Using Circulant Matrix h_temp = h; % Keep original h for shifting h_matrix = zeros(N, N); for n = 1:N h_matrix(:, n) = h_temp'; h_temp = circshift(h_temp, 1); % Circularly shift right end disp('Circulant Matrix:'); disp(h_matrix); y2 = h_matrix * x'; disp('Output by Method-2 (Matrix):'); disp(y2'); %% Verification if isequal(round(y1,10), round(y2',10)) disp('Both methods give the same circular convolution result.'); else disp('Mismatch between methods. Check implementation.'); end %% Plot the sequences n = 0:N-1; figure('Name', 'Circular Convolution', 'NumberTitle', 'off'); subplot(3,1,1); stem(n, x, 'filled'); title('Sequence x(n)'); xlabel('n'); ylabel('Amplitude'); grid on; subplot(3,1,2); stem(n, h, 'filled'); title('Sequence h(n)'); xlabel('n'); ylabel('Amplitude'); grid on; subplot(3,1,3); stem(n, y1, 'filled'); title('Circular Convolution y(n)'); xlabel('n'); ylabel('Amplitude'); grid on;