Script showing fourier transforms at arbitrary frequency,

allowing to determine the phase at any frequency without having to resort to interpolation of a DFT.
This commit is contained in:
Eric-Teunis de Boone 2022-11-10 15:05:45 +01:00
parent 205b67f691
commit 165a7c0361
2 changed files with 195 additions and 0 deletions

View file

@ -42,3 +42,44 @@ def ft_spectrum( signal, sample_rate=1, ftfunc=None, freqfunc=None, mask_bias=Fa
else:
return spectrum[1:], freqs[1:]
def ft_corr_vectors(freqs, time):
"""
Get the cosine and sine terms for freqs at time.
Takes the outer product of freqs and time.
"""
freqtime = np.outer(freqs, time)
c_k = np.cos(2*np.pi*freqtime)
s_k = np.sin(2*np.pi*freqtime)
return c_k, s_k
def direct_fourier_transform(freqs, time, samplesets_iterable):
"""
Determine the fourier transform of each sampleset in samplesets_iterable at freqs.
The samplesets are expected to have the same time vector.
Returns either a generator to return the fourier transform for each sampleset
if samplesets_iterable is a generator
or a numpy array.
"""
c_k, s_k = ft_corr_vectors(freqs, time)
if not hasattr(samplesets_iterable, '__len__') and hasattr(samplesets_iterable, '__iter__'):
# samplesets_iterable is an iterator
# return an iterator containing (real, imag) amplitudes
return ( (np.dot(c_k, samples), np.dot(s_k, samples)) for samples in samplesets_iterable )
# Numpy array
return np.dot(c_k, samplesets_iterable), np.dot(s_k, samplesets_iterable)
def discrete_fourier_properties(samples, samplerate):
"""
Return f_delta and f_nyquist.
"""
return (samplerate/(len(samples)), samplerate/2)