Created
November 21, 2025 08:51
-
-
Save Bugaddr/73b4ae9f9003ba1c2875cfeb170557ba to your computer and use it in GitHub Desktop.
Matlab code to generate circular convolution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| clc; | |
| clear; | |
| close all; | |
| %% 1. Define Input Sequences (From your specific problem) | |
| x = [1, 0, 1, 0]; % Outer Circle Sequence | |
| h = [1, 2, 3, 4]; % Inner Circle Sequence | |
| % Determine N | |
| N = max(length(x), length(h)); | |
| % Zero padding just in case (not needed here as both are 4) | |
| x = [x, zeros(1, N - length(x))]; | |
| h = [h, zeros(1, N - length(h))]; | |
| %% 2. Prepare Sequences | |
| % Outer circle: x(n) arranged clockwise | |
| outer_circle = x; | |
| % Inner circle Initial State (for y(0)): | |
| % Represents h(-n) mod N. | |
| % Index mapping: 0->0, 1->3, 2->2, 3->1 | |
| inner_circle = zeros(1, N); | |
| inner_circle(1) = h(1); % h(0) | |
| inner_circle(2:end) = h(end:-1:2); % h(3), h(2), h(1) ... | |
| %% 3. Compute and Visualize All Steps | |
| y = zeros(1, N); | |
| figure('Color', 'w', 'Position', [100, 100, 1000, 800]); | |
| sgtitle('Concentric Circle Method: x(n)=[1,0,1,0], h(n)=[1,2,3,4]', 'FontSize', 16); | |
| % Angles for plotting (Top, Right, Bottom, Left for N=4) | |
| % We go clockwise: 90, 0, -90, -180 | |
| angles = linspace(pi/2, pi/2 - 2*pi + 2*pi/N, N); | |
| for n = 0:N-1 | |
| % --- Calculation --- | |
| % 1. Multiply corresponding points | |
| product_seq = outer_circle .* inner_circle; | |
| % 2. Sum to get result | |
| y(n+1) = sum(product_seq); | |
| % --- Visualization (Subplot for this step) --- | |
| subplot(2, 2, n+1); | |
| axis equal; hold on; | |
| axis([-2.5 2.5 -2.5 2.5]); | |
| axis off; | |
| % Draw Circles | |
| theta = linspace(0, 2*pi, 100); | |
| plot(1.5*cos(theta), 1.5*sin(theta), 'b', 'LineWidth', 1.5); % Outer Ring | |
| plot(0.8*cos(theta), 0.8*sin(theta), 'r', 'LineWidth', 1.5); % Inner Ring | |
| % Plot Data Points | |
| for k = 1:N | |
| % Outer Circle Label (x) | |
| val_x = outer_circle(k); | |
| txt_x = sprintf('x(%d)=%d', k-1, val_x); | |
| text(1.8*cos(angles(k)), 1.8*sin(angles(k)), txt_x, ... | |
| 'Color', 'b', 'HorizontalAlignment', 'center', 'FontSize', 10, 'FontWeight', 'bold'); | |
| % Inner Circle Label (h rotated) | |
| val_h = inner_circle(k); | |
| % Note: The index of h displayed here is inferred for visualization logic | |
| % but simply showing the value is less confusing for the calculation check. | |
| txt_h = sprintf('%d', val_h); | |
| text(0.95*cos(angles(k)), 0.95*sin(angles(k)), txt_h, ... | |
| 'Color', 'r', 'HorizontalAlignment', 'center', 'FontSize', 10, 'FontWeight', 'bold'); | |
| % Connection Line (Multiplication) | |
| line([0.8*cos(angles(k)) 1.5*cos(angles(k))], ... | |
| [0.8*sin(angles(k)) 1.5*sin(angles(k))], 'Color', [0.5 0.5 0.5], 'LineStyle', ':'); | |
| % Show multiplication result at the edge if non-zero | |
| if (val_x * val_h) ~= 0 | |
| text(2.2*cos(angles(k)), 2.2*sin(angles(k)), sprintf('(%d)', val_x*val_h), ... | |
| 'Color', 'k', 'FontSize', 8, 'HorizontalAlignment', 'center'); | |
| end | |
| end | |
| % Title for this step | |
| title(sprintf('Step n=%d\nSum = %d', n, y(n+1)), 'FontSize', 12); | |
| % --- Prepare for Next Step --- | |
| % Rotate Inner Circle Clockwise (Shift Right) | |
| inner_circle = circshift(inner_circle, 1); | |
| end | |
| % Display Final Result in Console | |
| disp('---------------------------------------------'); | |
| disp('Calculated Circular Convolution y(n):'); | |
| disp(y); | |
| disp('---------------------------------------------'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment