0
\$\begingroup\$

I'm trying to calculate the phase of "rect" which is like below \begin{equation} f(x)=\begin{cases} 1 & \text{if }256<x<768,\\ 0 & \text{otherwise}. \end{cases} \end{equation} which is shifted rect to right with amount 512, so I expected to see a linear phase diagram which is like below $$ \text{phase} = -512\omega $$ but I am getting this:

phase of fourier

Why is it jumping when phase is equal to 1 or -1 radian (\$\pm \pi^{\circ}\$)?

This is the Matlab code I used to draw the plot:

rect_func = @(N,t) 1 .*((t>(N/4)) & (t < 3*N/4)) + 1/2 .*(t==N/4 | t==3*N/4);
x4 = @(t) rect_func(1024,t);
% plot 
time_step = 0.01;
t = 0:time_step:1024;
fs = 1/time_step;
x = x4(t);
f = linspace(-fs/2, fs/2, numel(x));
yFft = fft(x);
y = fftshift(yFft);
ph = angle(y);
plot(f,ph/pi);
xlim([-0.05,0.05]);
ylim([-1.1,1.1]);
\$\endgroup\$
5
  • 1
    \$\begingroup\$ It jumps at DC but not at \$\pi\$? Maybe you need to embed your picture and mark it up where you have an understanding problem? \$\endgroup\$
    – Andy aka
    Commented Nov 13, 2021 at 10:59
  • \$\begingroup\$ i meant it jumps at where the phase is equal to 1 radian. why? \$\endgroup\$ Commented Nov 13, 2021 at 11:07
  • \$\begingroup\$ Please edit in all the relevant Matlab(?) code used to generate this plot. It could be a plotting / interpreting issue ? When I plot angle(fft(shifted_rect_signal)), I get the jumps which are multiples of \$\pi\$. \$\endgroup\$
    – AJN
    Commented Nov 13, 2021 at 12:14
  • \$\begingroup\$ i added the code to question. \$\endgroup\$ Commented Nov 13, 2021 at 12:18
  • \$\begingroup\$ Sorry, I saw your update just now. You can se the @ in the comments to generate a notification to the person you are replying to like @amirhosseinbagheri \$\endgroup\$
    – AJN
    Commented Nov 28, 2021 at 14:05

1 Answer 1

2
\$\begingroup\$

I added a few modifications to your code so that the expected result is more visible.

  1. 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.

  2. 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;

phase response of shifted rectangular pulse

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\$.

\$\endgroup\$
1
  • \$\begingroup\$ thanks for your answer it helped a lot \$\endgroup\$ Commented Nov 29, 2021 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.