Continue to Site

# performing high precision fft in pyhon

Status
Not open for further replies.

#### yefj

Hello, i have build an amplifier which creates an analog signal at DC=1.8 and f=1Mhz with amplitude of 57.106822mV
I have sampled my signal at Ts=2.179398748773332e-08.
I want to do FFT with 1Hz bin so signal_duration = 45.8 million * 2.179398748773332e-08 seconds=1 sec as shown in the directive bellow.
so i expect to see FFT plot 0..45.8MHz(simtric plot)
i extracted the table and built FFT code in python as shown bellow.
As you can see my AC signal is displayed at 2MHz instead f 1MHz
Where did i go wrong?
Thanks.

Code:
from scipy.fftpack import fft
#import plotly
#import chart_studio.plotly as py
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor
#%matplotlib qt

array_fft=dataset_fft.values
Ts=array_fft[4][0]-array_fft[3][0];
Fs=1/Ts #Hz
L=np.size(array_fft)
freq_vec=Fs*np.arange(0,1,1/np.size(array_fft))
#L=np.size(freq_vec)/2
fft_y=fft(array_fft[:,1],L)
fig=plt.figure()
ax=fig.subplots()
ax.grid()
cursor=Cursor(ax, horizOn=True,vertOn=True,useblit=True,color='r',linewidth =1)
#ax.plot(array_fft[:,0],array_fft[:,1])
ax.plot(freq_vec,abs(fft_y)/np.size(array_fft))
plt.show()

Judging from your time domain each cycle is 1 microsec implying 1MHz.
If fft is saying it is 2MHz then check your Fs value and freq scaling from running code.

How do you generate the output file? Setting maximum timestep in transient simulation parameters doesn't generate a waveform file with equidistant sample points.

yefj

### yefj

Points: 2
Hi

When I see values with 15 decimals ... I automatically guess you don't know what you are doing.
Can you reproduce the value down to 4 decimals, or 5?

This also fits to a sampling rate of 45MHz and an FFT with 1Hz resolution.
I can't imagine an application where this makes sense.

Additionally expecting the FFT result to go up to 45MHz ... while the sampling rate is 45MHz...

****
Please decide what resolutions you really need

Basically most FFT algorithms expect an input value count of 2^n, so either 33.5 million or 67 million, but not 45 million.
The max output frequency is always below half sampling frequency.
While possible to use an FFT with millions of input samples, I can't remember seen one above 65k samples.
If I'm not mistaken then calculation time rises at least squared to input count.... so rising input count from 65k to 67 million may cause the calculation to increase by a factor of 1 million. Like from 10ms to 10,000s .. which means hours...
****

Klaus

Hello FVM, you are correct.
This is a problem.
How do i deside in LTSPICE the exact sampling rate of a my sinous signal?
I cant see how do i change the sample rate of this, or make it absolutely constant.Its not "equidistant".
Is there something you could reccomend me?
Thanks.

etting maximum timestep in transient simulation parameters doesn't generate a waveform file with equidistant sample points."

Status
Not open for further replies.