Line | |
---|
1 | function [npsdx, psdx, freq] = npsd(x)
|
---|
2 | %npsd - Normalized Power Spectral Density (using fft)
|
---|
3 | %
|
---|
4 | % x: time series
|
---|
5 | %
|
---|
6 | % psdx: Power Spectral Density(right half plane, value doubled)
|
---|
7 | % freq: frequency(non-negative half)
|
---|
8 | %
|
---|
9 | % Author: Cheng Gong
|
---|
10 | % Date: 2021-08-17
|
---|
11 |
|
---|
12 | % length of data
|
---|
13 | Nx = length(x);
|
---|
14 | % fft
|
---|
15 | xdft = fft(x);
|
---|
16 | xdft = xdft(1:floor(Nx/2+1));
|
---|
17 | % power specturm
|
---|
18 | psdx = (1/(2*pi*Nx)) * abs(xdft).^2;
|
---|
19 | % In order to conserve the total power, multiply all frequencies that
|
---|
20 | % occur in both sets - the positive and negative frequencies — by a factor of 2
|
---|
21 | psdx(2:end-1) = 2*psdx(2:end-1);
|
---|
22 | freq = 0:(2*pi)/Nx:pi;
|
---|
23 | % normalize(trapzoidal rule)
|
---|
24 | intS = trapz(freq, psdx);
|
---|
25 | if intS == 0
|
---|
26 | psdx = 1 +psdx;
|
---|
27 | intS = trapz(freq, psdx);
|
---|
28 | end
|
---|
29 | npsdx = psdx ./ intS;
|
---|
30 |
|
---|
31 |
|
---|
32 | end |
---|
Note:
See
TracBrowser
for help on using the repository browser.