Skip to content

Instantly share code, notes, and snippets.

@tarekeldeeb
Last active December 9, 2025 16:43
Show Gist options
  • Select an option

  • Save tarekeldeeb/23f81311129ecf500033f9ead456e97a to your computer and use it in GitHub Desktop.

Select an option

Save tarekeldeeb/23f81311129ecf500033f9ead456e97a to your computer and use it in GitHub Desktop.
This setup models a clean, discretized 3-phase system with controlled quantization and physically meaningful signal delay.
clear; clc;
%% PARAMETERS
N = 144; % samples per period
Nbins = 18; % 18 bins -> 9 per half cycle
L = N / Nbins; % samples per bin = 8
n = 0:N-1; % sample indices
w0 = 2*pi/N; % sine frequency
shift = N/3; % 120° = 48 samples = 6 bins
delay_cap = L/2 + 1; % requested delay = 5 samples
%% ---- Phase A base sine ----
xA = sin(w0 * n);
%% ---- Time quantization of Phase A into 18 bins ----
xA_blocks = reshape(xA, L, Nbins); % 8 x 18, each column is a bin
xA_bin = mean(xA_blocks, 1); % 1 x 18 bin averages
%% ---- ENFORCE PERFECT SYMMETRY ON BIN LEVELS ----
% Positive half: bins 1..9
pos = xA_bin(1:9); % [a b c d ~1 d' c' b' a']
% Symmetrize around centre (index 5)
for k = 1:4
j = 10 - k; % mirror index (9..6)
v = 0.5 * (pos(k) + pos(j)); % average the pair
pos(k) = v;
pos(j) = v;
end
% Force centre bin to exactly 1 (optional but nice)
pos(5) = 1.0;
% Build full symmetric 18-bin pattern
xA_bin_sym = zeros(1, Nbins);
xA_bin_sym(1:9) = pos; % + half
xA_bin_sym(10:18) = -pos; % exact negative half
% Expand to staircase for Phase A
xA_q = repelem(xA_bin_sym, L); % 1 x 144
%% ---- 3-phase quantized stairs (just shifts of the same pattern) ----
xB_q = circshift(xA_q, shift); % 120°
xC_q = circshift(xA_q, 2*shift); % 240°
%% ---- 3-phase *true* sines (for contour) ----
xB = circshift(xA, shift);
xC = circshift(xA, 2*shift);
%% ---- Causal delay (no wrap-around) for contours ----
delay_causal = @(x, init, D) [ repmat(init, 1, D), x(1:end-D) ];
xA_d = delay_causal(xA, xA_q(1), delay_cap);
xB_d = delay_causal(xB, xB_q(1), delay_cap);
xC_d = delay_causal(xC, xC_q(1), delay_cap);
%% ---- Quantization level lines (9 levels) ----
pos_levels = sort(unique(pos)); % a,b,c,d,1
all_levels = [-fliplr(pos_levels), 0, pos_levels];
%% ---- PLOT EVERYTHING ----
figure; hold on;
% Quantized stairs
stairs(n, xA_q, 'LineWidth', 2, 'Color', [1 0 0]); % Phase A
stairs(n, xB_q, 'LineWidth', 2, 'Color', [0 0.6 0]); % Phase B
stairs(n, xC_q, 'LineWidth', 2, 'Color', [0 0 1]); % Phase C
% Delayed contour sines
plot(n, xA_d, 'o', 'Color', [1 0 0], 'MarkerSize', 4, 'LineStyle', 'none');
plot(n, xB_d, 's', 'Color', [0 0.6 0], 'MarkerSize', 4, 'LineStyle', 'none');
plot(n, xC_d, 'd', 'Color', [0 0 1], 'MarkerSize', 4, 'LineStyle', 'none');
% Level lines
for Lvl = all_levels
yline(Lvl, '--', 'Color', [0.5 0.5 0.5 0.5], 'LineWidth', 1);
end
title('3-Phase Symmetric Stair Sine (N = 144, 18 bins) with Delayed Contours');
xlabel('Sample index n');
ylabel('Amplitude');
legend(...
'Phase A quantized', 'Phase B quantized', 'Phase C quantized', ...
'Phase A delayed', 'Phase B delayed', 'Phase C delayed' ...
);
grid on;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment