# length of a signal when doing fft and power spectrum analysis

5 views (last 30 days)

Show older comments

I am undertaking experiments on a sandpile to understand self organised criticality and therefore want to plot a power spectrum of my data.

I have the following code which plots a power spectrum for me, however I was wondering if somebody could explain about the length of the signal and sampling frequency.

- Signal length (N): My data consists of the number of grains leaving the sandpile every second. The sandpile is supplied at a constant rate. Is the signal length just the length of time the experiment was run for?
- Sampling frequency (fs): It says that sampling frequency is how many samples are taken per second, does this mean that if I collect data from my experiment every second, then fs = 1?

%fft/power spectra

%set up basic parameters

signal = grains;

N = length(signal); %number of samples in the signal

fs = 1; %how many samples are taken per second

fnyquist = fs/2; %nyquist frequency

%plot a single sided power spectrum with log frequency azis

%abs is the magnitude values

%fft is a built in function that determines magnitude and phases of sinusoids present in a signal

X_mags = abs(fft(signal));

%assign frequency axis values, starting with bins from 0:N-1

bin_vals = [0 : N-1];

%to convert from bin to Hz use the standard formula: bin*fs/N, where fs and

%N are defined at the beginning

fax_Hz = bin_vals*fs/N;

%ceil rounds each element of N/2 to the nearest integer greater or equal to that element

%e.g. -1.9 is rounded to -2.0

N_2 = ceil(N/2);

%create a plot where the x axis has a log scale (semilogx)

%x axis is log Hz, ranging from 1:N2

%y axis converts the values of X_mags to dB using 20*log10

figure(2)

semilogx(fax_Hz(1:N_2), 20*log10(X_mags(1:N_2)))

xlabel('Frequency (Hz)')

ylabel('Power (dB)');

title({'Single-sided Power spectrum' ...

' (Frequency in shown on a log scale)'});

axis tight

##### 1 Comment

dpb
on 26 Nov 2020

### Answers (0)

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!