I'm trying to learn more about noise, power spectral denisty (PSD) and statistical variances. With regards this I'm trying to compute the Power Spectral density of white noise, however, when I do I get a very odd symmetry. my spectrum seems to be symmetric around the central frequency value, which is obviously incorrect. I'm new to using autocorrelations and Power spectral densities so I'd appreciate if someone could help nudge me in the direction of the error.
Code to calculate PSD:
import numpy as np
from math import sin, pi, log10
from allan_noise import white,pink,brown, violet
import acor
import numpy as np
#METHOD ONE: AUTOCORRELATION FUNCTION METHOD
def psd1(time_dats):
#auto_corr = acor.function(time_dats)
auto_corr = np.correlate(time_dats,time_dats,mode="same")
#To check autocorrelation
for i in range(len(auto_corr)):
print i*.0000001 ,auto_corr[i]
return fft(auto_corr)
#DEFINE VARIABLES
t = .0001 #simulation time
N = 100000 #number of data points
dt = t/N #time interval between data points
#CREATE SIGNAL
sig = white(N)
df = 1.0/(t)
freq_axis = np.arange(0,N/t,df)
spec = psd1(sig)
#OPEN UP OUTPUT FILE
f = open('data/psdtest_f','w')
g = open('data/psdtest_t','w')
#PRINT OUT DATA
for i in range(N):
f.write('%g %g\n' %(freq_axis[i],log10(abs(spec[i]))))
g.write('%g %g\n' %(i*dt, sig[i]))
Using this code I produce the following graphs, which can be accessed here https://drive.google.com/#folders/0B8BdiIhqTYw7Vk1EaDFMQW84RHM :
Temporal Profile of the noise before calculations
Autocorrelation function calculated from the temporal profile (I'm aware the x axis scale is wrong but that doesn't contribute to the code any where else
Power Spectral Denisty, beautifully symmetric though not supposed to be
Any help anyone could provide as to whats causing this symmetry would be most helpful. i've tested the code with a simple sin wave signal and I get expected results (with no symmetry)
First,your function is written incorrect, you take fft from autocorr which is not your initial signal array. Funny thing, because of the nature of rounding error you will get whit noise like psd from it as well. Second, you calculating frequencies axis wrong, as they should be extended to N/(t*2) (which is Nyquist fr.). Instead, since your freq_axis array length is N, you retrieve N elements from you sig array, and because of that you just read negative frequencies with the same psd, which causes you the symmetry (taking log convert you variable to real and all the Fourier coefficients for negative freqs are just conjugates for positive ones). replacing your code with the following one give a perfectly good result:
sig = white(N,1,N/t)
(siglog,freq_axis)=ml.psd(sig,N,(N/t), detrend=ml.detrend_none,
window=ml.window_hanning, noverlap=0, pad_to=None,
sides='default', scale_by_freq=None)
plt.plot(freq_axis,np.log10(siglog))
plt.show()
don't forget to import
import matplotlib.mlab as ml
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.