-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve spectrum estimation 'wrong' #947
scale DC and Nyquist frequency by just /N not 2/N
- Loading branch information
Showing
1 changed file
with
10 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a9cdf88
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I understand now, and I may have formalated the issue wrong: It is the energy that is maintained (spectral power) and not the amplitude
I did the folloing test
import numpy as np
import scipy.fft
x=np.array([0,0,4,0,0,0,0])
N=len(x)
def Power(x,N): return np.sum(np.abs(x)**2,axis=0)/N
x1=scipy.fft.fft(x)
print(N,x1)
print(Power(x,1),Power(x1,N))
x2=scipy.fft.rfft(x)
n2=N//2
if N%2==1: n2 +=1
x2[1:n2] *=np.sqrt(2)
print(N//2,x2)
print(Power(x,1),Power(x2,N))
to demonstrate the proper scaling
In your case I would suggest
N = len(self.samples)
fft = scipy.fft.rfft(self.samples)
fft = np.abs(fft) **2/ N # scale here all spectral bins
n2=N//2
if N%2==1: n2 +=1 # estimate number of frequencies excluding Nyquist (happens when N is even)
fft[1:n2] *=2 # correct for negative frequencies
and Level_dB = 10*np.log10(fft)