Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Phase aliasing ambguity and simulation in MatLab?

Status
Not open for further replies.

TicTek

Newbie level 6
Joined
May 10, 2022
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
211
Hello Guys,
So after reading this article Sampling and Aliasing at page 30; I could not reproduce the where the phase has a conjugate value of the origial phase at Fs/2<f<Fs which result in :
fa = Fs − f, fa is the aliased frequency
Za = Z

so my code is here:
Code:
 Fs1=30;  % original sampling frequency
t1=0:1/Fs1:1;
f1=4;ph1=-pi/4;  % signal frequency
x1=sin(2*pi*f1*t1+ph1);   % original signal with original sampling frequency Fs1


Fs2=6;% variable sampling frequency
t1=0:1/Fs1:1;
t2=0:1/Fs2:1;
x2=sin(2*pi*f1*t2+ph1);  % original signal with variable sampling frequency Fs1

subplot(2,1,1)
plot(t1,x1)
subplot(2,1,2)
plot(t2,x2 )

How can I get like him where I can visualize the phase ?
1659428514270.png
 

coding for lowest waveform (Fs = 5 Hz, f = 1hz or 4 Hz)

Fs=5; %sampling frequency
t=0:1/Fs:Fs;
f=1;
ph=+pi/4;
x1=cos(2*pi*f*t+ph);

f = 4;
ph = -pi/4;
x2=cos(2*pi*f*t+ph);

plot(x1);hold;
plot(x2,'r--')
 

coding for lowest waveform (Fs = 5 Hz, f = 1hz or 4 Hz)

Fs=5; %sampling frequency
t=0:1/Fs:Fs;
f=1;
ph=+pi/4;
x1=cos(2*pi*f*t+ph);

f = 4;
ph = -pi/4;
x2=cos(2*pi*f*t+ph);

plot(x1);hold;
plot(x2,'r--')
Thank you.
But the theory here says if I change the sampling frequency without changing f(must be set to 4 as constant) we should observe a phase conjugate due to low sampling rate
 

Thank you.
But the theory here says if I change the sampling frequency without changing f(must be set to 4 as constant) we should observe a phase conjugate due to low sampling rate
What I see on the waveforms is that:
4Hz @ 30 Fs, no issue of aliasing
4Hz @ 11 Fs, no issue of aliasing
4Hz @ 10 Fs, no issue of aliasing
4Hz @ 6 Fs, aliasing(alias at 2Hz)
4Hz @ 5 Fs, aliasing (alias at 1 Hz)

I took last case and the alias would be at 1Hz with phase as coded.
So I generated 4Hz @ 5 Fs and 1 Hz @ 5 Fs and checked phase
 
Hi,

"phase" is a measure from one signal to another, and both need to have the same frequency.
Phase is not an absoulte measure.
Even in mathematics when we say V_ac = amplitude x sin(w t)
t is a victive point of time when the signal(math) starts.

And in your case I see two independent frequencies. (unless they are dreived from the same frequency source):
* the sampling frequency
* and the signal frequency
As soon as they are independent .. the will drift with respect to each other. which makes the value of a phase angle invalid.

An example:
4Hz signal with at 30 Hz sampling frequency.
in a simlator you can start both at exactly the same time, and you can ste the frequency with zero error.
the 4Hz signal starts at phi = 0 and the sampling frequency starts a phi = 0.
You may refer to this idealistic scenario: with non realistic conditions.

Now we use the same example with the same idealistic frequencies. Just the first ADC sample is neglected.
Again: both signals are not modified. You see the same signals but 33ms later.
* the sampling signal still is at 0° (indeed 360°, but in a circle we just have 0° to 360°)
* but the the 4Hz signal now is at 12°

So we don´t only have the
* signal frequency
* sampling frequency
* (even if they start with 0° at idealistically the same time)
* digital data offset

Indeed I find it difficult to talk about "phase" between two different frequencies.


Klaus
 
Hi,

"phase" is a measure from one signal to another, and both need to have the same frequency.
Phase is not an absoulte measure.
Even in mathematics when we say V_ac = amplitude x sin(w t)
t is a victive point of time when the signal(math) starts.

And in your case I see two independent frequencies. (unless they are dreived from the same frequency source):
* the sampling frequency
* and the signal frequency
As soon as they are independent .. the will drift with respect to each other. which makes the value of a phase angle invalid.

An example:
4Hz signal with at 30 Hz sampling frequency.
in a simlator you can start both at exactly the same time, and you can ste the frequency with zero error.
the 4Hz signal starts at phi = 0 and the sampling frequency starts a phi = 0.
You may refer to this idealistic scenario: with non realistic conditions.

Now we use the same example with the same idealistic frequencies. Just the first ADC sample is neglected.
Again: both signals are not modified. You see the same signals but 33ms later.
* the sampling signal still is at 0° (indeed 360°, but in a circle we just have 0° to 360°)
* but the the 4Hz signal now is at 12°

So we don´t only have the
* signal frequency
* sampling frequency
* (even if they start with 0° at idealistically the same time)
* digital data offset

Indeed I find it difficult to talk about "phase" between two different frequencies.


Klaus
It is not about phase of different frequencies. In my coded example it is about same frequencies as either 1 Hz @ 5 Fs or 1 Hz (as alias of 5-4=1).
 
Hi,
It is not about phase of different frequencies
if it´s not about different frequencies, then it has to be about identical frequencies.

Where do you have identical frequencies. I don´t see them.

Klaus
 
What I see on the waveforms is that:
4Hz @ 30 Fs, no issue of aliasing
4Hz @ 11 Fs, no issue of aliasing
4Hz @ 10 Fs, no issue of aliasing
4Hz @ 6 Fs, aliasing(alias at 2Hz)
4Hz @ 5 Fs, aliasing (alias at 1 Hz)

I took last case and the alias would be at 1Hz with phase as coded.
So I generated 4Hz @ 5 Fs and 1 Hz @ 5 Fs and checked phase
I understood that. What I am trying to see is how the last 2 case introduce phase aliasing by itself. You did write the -PI/4 in the code, is t phase aliasing happening automatically in case 4 and f?
--- Updated ---

Hi,

"phase" is a measure from one signal to another, and both need to have the same frequency.
Phase is not an absoulte measure.
Even in mathematics when we say V_ac = amplitude x sin(w t)
t is a victive point of time when the signal(math) starts.

And in your case I see two independent frequencies. (unless they are dreived from the same frequency source):
* the sampling frequency
* and the signal frequency
As soon as they are independent .. the will drift with respect to each other. which makes the value of a phase angle invalid.

An example:
4Hz signal with at 30 Hz sampling frequency.
in a simlator you can start both at exactly the same time, and you can ste the frequency with zero error.
the 4Hz signal starts at phi = 0 and the sampling frequency starts a phi = 0.
You may refer to this idealistic scenario: with non realistic conditions.

Now we use the same example with the same idealistic frequencies. Just the first ADC sample is neglected.
Again: both signals are not modified. You see the same signals but 33ms later.
* the sampling signal still is at 0° (indeed 360°, but in a circle we just have 0° to 360°)
* but the the 4Hz signal now is at 12°

So we don´t only have the
* signal frequency
* sampling frequency
* (even if they start with 0° at idealistically the same time)
* digital data offset

Indeed I find it difficult to talk about "phase" between two different frequencies.


Klaus
Thank you for your feedback.
what I am trying to see by simulating is the phase aliasing due to the lower frequency when Fs/2<f<Fs as stated in the article. That means I have first simulated the original signal with Fs1=30,f=4,Ph1=Pi/4, so this is my signal reference.
I created a second signal where I want to compare it to with Fs2=6,f=4H whith fa=2 . because Fs/2<f<Fs. this leads to have a ph2=-ph1.
 
Last edited:

Hi,

this leads to have a ph2=-ph1.
Yes, in the example. But in reality you will see different phases.

While one can clearly determine the alias frequency...
I say one can not clearly determine the alias frequency´s phase.

Klaus
 
I understood that. What I am trying to see is how the last 2 case introduce phase aliasing by itself. You did write the -PI/4 in the code, is t phase aliasing happening automatically in case 4 and f?
--- Updated ---
Let us try understand what we are after. Your top waveforms say:

4Hz @ -pi/4 phase, at Fs = 30Hz looks like 4Hz @ phase of -pi/4
4Hz @ -pi/4 phase, at Fs = 11Hz looks like 4Hz @ phase of -pi/4
4Hz @ -pi/4 phase, at Fs = 10Hz looks like 4Hz @ phase of -pi/4


Can you explain above strange wording. 4Hz @ Fs looks like 4Hz @ Fs.

These last two wordings make some sense:
4Hz @ -pi/4 phase, at fs = 6Hz looks like 2Hz @ phase of +pi/4
4Hz @ -pi/4 phase, at fs = 5Hz looks like 1Hz @ phase of +pi/4


Though based on KLausST "ambiguity of aliased phase theory" we better give up this thread.

You have also added two red arrowed lines?? what do they mean?
 

Thank you both. Those two arrows are explained as that whenever we have a frequency d which is between certain value, with the violation of Nyquist criteria, we get a phase aliasing. my objective was to simulate that in Matlab for better understanding
1660117180676.png
 

Hi,

Both frequency results are correct.
Phi can not be determined at all.
What does this mean: "the sign of all sines will be reversed" ?


How I do it:
* (1) all frequencies above fs/2 become mirrored at fs/2: --> fa = fs -f
* (2) all negative frequencies become positive: --> fa = -fa
Do both until: 0 <= fa < fs/2

Example1: fs = 2kHz, f = 1.1kHz
--> fs/2 = 1kHz,
--> (1) fa = f - fs/2 = 2kHz - 1.1kHz = 0.9kHz
finished

Example2: fs = 2kHz, f = 2.2kHz
--> fs/2 = 1kHz,
--> (1) fa = f - fs/2 = 2kHz - 2.2kHz = -0.2kHz
--> (2) fa = -fa --> -0.2kHz --> 0.2kHz
finished

Klaus
 

Hi,

Both frequency results are correct.
Phi can not be determined at all.
The question is not about frequency but phase.
The link document and my previous code showed that phase aliasing has its rule and is not undefined.

here is again another code for 100 samples:

y1=sin(2*pi*(0:99)*1/6); %F = 1, Fs = 6 (1st Nyquist zone, no aliasing)
y2=sin(2*pi*(0:99)*5/6); %F = 5, Fs = 6 (2nd Nyquist zone leads to inversion)
plot(y1);hold;
plot(y2,'r--');

y3=sin(2*pi*(0:99)*7/6); %F = 7, Fs = 6 (3rd Nyquist zone leads to double inversion)
figure;
plot(y1);hold;
plot(y3,'g--');
 
Last edited:

How can I get like him where I can visualize the phase ?
To reproduce those graphs exactly as presented, you can do this:

Code:
close all; clear all; clc;

% Time to simulate (seconds)
Tsim = 1;

% Sinewave frequency (Hz) and initial phase (radians)
fsine = 4;
phi0 = -pi/4;

% Vector of all sample rates (Hz)
fsamp = [30, 11, 10, 6, 5];

% For each sample rate...
Nrates = length(fsamp);
for i = 1:Nrates
    % Calculate number of samples to simulate
    Nsamp = floor(Tsim * fsamp(i)) + 1;
 
    % Calculate the sampled sinewave
    t = (0:Nsamp-1) / fsamp(i);
    x = cos(phi0 + 2*pi*fsine*t);
 
    % Calculate the aliased frequency (Hz) and phase (radians)
    fa = fsine;
    pa = phi0;
    while fa > fsamp(i)/2
        fa = fa - fsamp(i);
        pa = -pa;
    end
    fa = abs(fa);
 
    % Approximate an "analog" sinewave using a high sampling frequency
    fanalog = max(fsamp) * 1000;
    Nanalog = floor(Tsim * fanalog) + 1;
    tanalog = (0:Nanalog-1) / fanalog;
    xanalog = cos(pa + 2*pi*fa*tanalog);
 
    % Plot
    subplot(Nrates,1,i);
    hold on;
    stem(t, x, '.b', 'LineWidth', 1);
    plot(tanalog, xanalog, '--k', 'LineWidth', 1);
    line([0,Tsim], [0,0], 'Color', 'r');
    title([num2str(fsine) ' Hz at \pi / (' num2str(pi/phi0) ') phase, at Fs = ' num2str(fsamp(i)) ...
        ' Hz, looks like ' num2str(fa) ' Hz with a phase of \pi / (' num2str(pi/pa) ')']);
end

end

aliasing.png


However, in my opinion, the lecture slides are a little misleading. I personally don't find it helpful to think about this as "phase aliasing" at all. I find it much easier to simply consider that the frequency can wrap to a negative frequency.

In other words, I would remove this:

Code:
    % Calculate the aliased frequency (Hz) and phase (radians)
    fa = fsine;
    pa = phi0;
    while fa > fsamp(i)/2
        fa = fa - fsamp(i);
        pa = -pa;
    end
    fa = abs(fa);

and replace it with this:

Code:
    % Calculate the aliased frequency (Hz) and phase (radians)
    fa = fsine;
    while fa > fsamp(i)/2
        fa = fa - fsamp(i);
    end
    pa = phi0;

This produces the same graphs, but now the "aliased phase" is always exactly equal to the initial phase of -π/4 and we instead get negative aliased frequencies for the last two graphs:

aliasing_improved.png


For a cosine, the two are mathematically equivalent, so it is up to you which you prefer.

It is easy to prove that they are equivalent using these standard trigonometric identities:

\[ \cos(−x)=\cos(x) \]
\[ \sin(−x)=-\sin(x) \]
\[ \cos(\phi_0+\omega t) = \cos(\phi_0)\cos(\omega t) - \sin(\phi_0)\sin(\omega t) \]

So it is easy to see that for a cosine negating the angular frequency \(\omega = 2\pi f\) has exactly the same effect as negating the initial phase \(\phi_0\).

However, I think my version is more correct because it is also true for sine. (You can test this by changing the two "cos()" calls to "sin()" in the MATLAB script).
 
Last edited:

Edit: Please remove the final end from the main code snippet. This forum has a bug that sometimes causes text to be duplicated.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top