# Computing the Power Spectrum Density (PSD) on a CSV File in Python

im quit new with signal processing and im trying to calculate the PSD of a signal im sampling. the signal is an output of a DC buck converter

this is the code im using and this is the plot im getting

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

filename = 'scope_yos2.csv'
ConvertToMatrix = data.values
time = np.delete(ConvertToMatrix,[1,2],axis=1)
voltage = np.delete(ConvertToMatrix,[0,2],axis=1)
NumOfSampels= len(voltage)
plt.plot(voltage)
voltage1 = voltage.transpose()
samplFreq = 1.25e9
Pxx, freqs = plt.psd(voltage1,
NFFT=256,Fs=samplFreq,
detrend=mlab.detrend_mean,
window=mlab.window_hanning,noverlap=0,sides='onesided',scale_by_freq=True,
return_line=None)
plt.show()


first image is the DCDC ouput second one is the PSD plot

the csv file contains two columns first is the time ax and the second one is the amplitude

1. how come the plot is so smooth. this is the plot im getting using the welch algoenter image description here

also theres a argument called **kwargs , what is used for ? , as you can see im not using it

thanks

• **kwargs is the the argument to pass in a variable number of arguments. The first plot is smooth because the FFT length is short so the frequency resolution is relatively much larger. The FFT length used in the second plot was much longer to give the distinct frequency points shown. (explained in more detail in my answer). – Dan Boschen Aug 21 '18 at 11:46

I have not used the mlab functions in matplotlib, but from what I can discern from your time plot, I see a pattern that repeats roughly every 15,000 samples, at 1.25 Gsps according to your code.

This means we should see a strong spectral component at $1.25e9/15000=83.3$ KHz.

The reason we do not see this, I suspect is the relatively short FFT length (or very high sampling rate compared to the spectral occupancy, or both!).

Let me explain: The FFT length is currently 256 samples. At 1.25GHz this is only $256/1.25e9 = 204.8$ ns.

The frequency resolution prior to using any additional window (meaning using a rectangular window) is $1/T$ where T is the length of the FFT in time (for further detail on that see: What happens when N increases in N-point DFT )

So in your case the frequency resolution is greater than $1/204.8e-9$ = 4.88 MHz!! Any window will reduce sidelobes but at the expense of increasing the mainlobe, so the width of your mainlobe given the Hanning window used will cause the resolution to be even wider than 4.88 MHz. (But definitely good to include windowing, otherwise the additional sidelobes would be much higher). You can see this post for additional details on the windowing choices:Why would one use a Hann or Bartlett window? (From this we see the Hanning window is approximately twice as wide as the rectangular window, so your example would have approximately 10 MHz of resolution bandwidth per bin in the FFT)

Looking at your plot, it looks like we see exactly this, which is the FFT bin of your strong DC component, and this single FFT bin is approximately 10MHz wide in bandwidth! So that is burying any signals of interest that you want to see.

My recommendation is to increase the FFT length within what your processing will allow. And if that starts to get problematic then reduce the sampling rate to get further resolution (however there are challenges in doing that properly that I won't get into except to say that you must be sure higher frequency components are filtered out prior to resampling). Without requiring a sampling rate change, you would want to use an FFT length of at least $1.25e9/10e3$ = 125,000 to get 20 KHz of resolution BW while using the Hanning window. That's a reasonable length and I suspect you can go much higher than that for finer resolution.

Also as a side note I don't see where the impedance is set, so I guess it is assumed to be normalized to 1 ohm. Just mentioning in case you cared about knowing the actual power levels.

• thanks, i did applied your tip, but still im getting the same plot – Yossi Israel Aug 21 '18 at 11:33
• Looks like you posted an answer as an extension to your question. You should delete that and instead edit your original question and put it there. That new plot does look much better with finer frequency resolution indicating you used a longer FFT. (But your frequency axis does not appear to be scaled correctly, with the highest frequency show around 1.25E-3 MHz?) – Dan Boschen Aug 21 '18 at 11:36
• But are you saying that you set NFFT=125000 and you get the exact same plot? That woud not make sense and I would suspect an error in what you actually changed or for some other reason the FFT length is getting forced to the default length of 256. – Dan Boschen Aug 21 '18 at 11:43
• that was done in a colleague code to compare the results, im now using a NFFT of 4096 ,also changed the sampling rate to 2500 – Yossi Israel Aug 21 '18 at 11:47
• 4096 is only 16 times longer, so your frequency resolution in that case would only be 625KHz with the same window and sampling rate. Looking at your colleague's plot and the width of the narrow frequency spikes, his frequency resolution is much more narrow than this. (So the FFT used must have been much longer or sampling rate much lower). I see now you said you also reduced the sampling rate: be careful reducing the sampling rate that you don't go below twice the frequency of your dominant spectrum. – Dan Boschen Aug 21 '18 at 11:50