I added a few modifications to your code so that the expected result is more visible.
The first modification is that, I reduced the width of the rectangular pulse (from 50/100 to just 4/100) so that the phase of the non-shifted pulse has enough samples in it to show the original phase behaviour. Original rectangular pulse lasted a duration of half the number of samples which made its fundamental component and the first few significant harmonics spaced very very close together in the frequency domain samples. So the phase behaviour of the non shifted rectangular pulse itself would have been difficult to see (let alone that of the shifted pulse). By reducing the width of the pulse, the fundamental frequency as well as the significant harmonics are placed sufficiently far apart in the frequency samples.
The second modification is that, I reduced the phase shift also (25/100 to 2/100). a phase shift of 512w
would quickly cause the phase response to wrap around \$\pi\$. But with the smaller phase shift, the small increase in the phase response slope is quite clear.
The modified part of your code and the resulting figure are shown below.
The unshifted pulse as well as the shifted pulse are both shown to clearly ilustrate the additional linear variation in slope which you expected.
rect_func = @(N,t) 1 .*((t>(2*N/100)) & (t < 6*N/100)) + 1/2 .*(t==2*N/100 | t==6*N/100);
subplot(3,1,1);
plot(t, x, '-.');
hold on;
subplot(3,1,2);
plot(f,ph/pi, '.-', 'markersize', 10);
hold on;
xlim([-0.05,0.05]);
ylim([-1.1,1.1]);
subplot(3,1,3);
plot(f,abs(y), '.-', 'markersize', 10);
xlim([-0.05,0.05]);
hold on;
note
Since you are constructing the phase from the complex numbers in the fft
result, the phase values will be restricted to \$\pm\pi\$. If the phase changes too rapidly (as in the case of 512w
), then the values are wrapped around in a small number of samples in the fft
and the expected linear change of phase wont be visible. The line gets broken and shifted by \$\pm \pi\$.
angle(fft(shifted_rect_signal))
, I get the jumps which are multiples of \$\pi\$. \$\endgroup\$