I posted this question on StackOverflow as well (link), but it is somewhere between a math question and a programming question (I'm looking for some formulas regarding aliasing frequencies and I want to implement them using Python code), so I thought I'd post it here too, as I haven't found my answer yet. Since I am mostly copying this post from the original Stackoverflow thread, it is phrased as more of a programming question. The question is...
In Python, I'm trying to write an algorithm alias_freq(f_signal,f_sample,n), which behaves as follows:
def alias_freq(f_signal,f_sample,n):
f_Nyquist=f_sample/2.0
if f_signal<=f_Nyquist:
return n'th frequency higher than f_signal
that will alias to f_signal
else:
return frequency (lower than f_Nyquist)
that f_signal will alias to
The following is code that I have been using to test the above function (f_signal, f_sample, and n below are chosen arbitrarily just to fill out the code)
import numpy as np
import matplotlib.pyplot as plt
t=np.linspace(0,2*np.pi,500)
f_signal=10.0
y1=np.sin(f_signal*t)
plt.plot(t,y1)
f_sample=13.0
t_sample=np.linspace(0,int(f_sample)*(2*np.pi/f_sample),f_sample)
y_sample=np.sin(f_signal*t_sample)
plt.scatter(t_sample,y_sample)
n=2
f_alias=alias_freq(f_signal,f_sample,n)
y_alias=np.sin(f_alias*t)
plt.plot(t,y_alias)
plt.xlim(xmin=-.1,xmax=2*np.pi+.1)
plt.show()
My thinking is that if the function works properly, the plots of both y1 and y_alias will hit every scattered point from y_sample. So far I have been completely unsuccessful in getting either the if statement or the else statement in the function to do what I think it should, which makes me believe that either I don't understand aliasing nearly as well as I want to, or my test code is no good. What I'm looking for is formulas that I can implement in both of these parts of the code, but so far the only one I've found, which would go in the else statement, doesn't really work (see first answer on link to Stackoverflow version of this question).
My questions are:
Prelimarily, is the test code I'm using sound for what I'm trying to do? And primarily, what is the alias_freq function that I am looking for?
Thanks.
Question: What are the alias frequencies of a signal of single frequency $f_0$ $$ X(f) = \delta(f - f_0) $$ sampled with frequency $f_s$?
Answer: The alias frequencies are from the set $$ A = \{ f_a(N) := \lvert f_0 - N f_s \rvert : N \in \mathbb{Z} \} $$ See sampling sinusoidal functions.
Note: This statement can be tested by choosing some $f_0$ and $f_s$, then calculating some alias frequency $f_a$. The two signals at $f_0$ and $f_a$ are then sampled at $f_s$. If the statement is true, the samples should not differ. (This also means that both sigmals should yield identical reconstructions.)