fft scaling with python

28 Views Asked by At

I have a given signal ds and I want to compute its scaled FFT. By that I mean that I want to get the FFT but I want the frequencies axis to be in the range of [-1,1]. My question is how could I do that?

Here is my code:

contour_fft = np.fft.fft(ds)
contour_fft = np.fft.fftshift(contour_fft)
magnitude = abs(contour_fft)
sample_rate = 1.0
N = len(ds)
freq = np.fft.fftfreq(N, sample_rate)
freq = np.fft.fftshift(freq)

# Plot the magnitude
plt.figure(figsize=(50, 5))
plt.plot(freq, magnitude)
plt.axhline(0, color='black', linestyle='--', linewidth=1, label='Zero Magnitude')
plt.xlabel('Frequency')
plt.ylabel('Contour FFT Magnitude')
plt.legend()
plt.title('Contour FFT Magnitude Plot')
plt.grid(True)
plt.show()

I tried to change the sample rate of freq=np.fft.fftfreq(N, sample_rate), and it did scaled the fft different, but I did not know how to change it to be in the range of [-1,1].

1

There are 1 best solutions below

0
Guy On

You can generate N random numbers in this range, fftfreq can get a scalar in the d parameter

N = len(ds)
sample_rate = np.random.uniform(-1, 1, N).round(2)

# sample_rate = [ 0.81  0.06  0.34 -0.28 -0.35  0.96 -0.77  0.48  0.07 -0.09]