## How to plot power spectrum

Anything that doesn't fit elsewhere.
Posts: 50
Joined: Thu Jul 07, 2011 6:20 pm

### How to plot power spectrum

Hi,
I am trying to analyse spectrum of a signal (power vs freq.), and I find that fft() and FFT() functions need some post-processing to do this. There is a function spctrm(), but its not clear from instructions how to use it. Are there any libraries for advanced signal analysis? Ideally, a procedure which takes (source_signal, sample_frequency, window_length, window_overlap) and plots a spectrum? Or I have to switch to Matlab for this stuff?
Thanks!
ted
Posts: 5793
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

### Re: How to plot power spectrum

spctrm generates results identical to the procedure of the same name described in Numerical Recipes in C (Press et al., Cambridge University Press), with Bartlett window and overlapping data segments. NEURON's spctrm differs in the following ways:
1. It takes only one argument--a vector of data sampled at regular intervals, which may contain any number of samples.
2. It handles zero padding and selects the number of data segments automatically. Consequently the number of frequency bins, and the number of data segments that are averaged to calculate the power density in these bins, are not subject to user control--see Relationship between sample length and results below.

Given that the sample interval is dt, the Nyquist frequency is 1/(2*dt).
Given that the number of frequency bins is numf ( == length of result vector), then the frequency bin centers are at i/(2*dt*numf) where i = 0..numf-1. That is, the result vector contains signal power in numf frequency bins centered at 0, 1/(2*dt*numf), . . (numf-1)/(2*dt*numf).

Relationship between sample length and results

I ran some tests and found that the data vector must contain at least 16 samples in order to obtain a result vector with at least two elements (power at 0 Hz (DC) and one nonzero frequency). Furthermore, the number of frequencies in the result vector is a monotonically increasing staircase function of the number of data samples in the argument vector. The relationship between argument length and result length is summarized by this table, which lists the argument lengths (number of samples or "# S") at which the result length (number of frequencies or "# F") increases:

Code: Select all

``````# S    # F
16      2
24      4
40      8
72     16
136     32
264     64
520    128
1032    256``````
One last note: there are many different ways to analyze the spectral content of signals, each with its own particular strengths and weaknesses. Almost certainly some of these have been implemented with Python, and should be callable directly from hoc.