# pwelch

Computes the power spectral density.

## Syntax

Pxx=pwelch(x)

Pxx=pwelch(x,window)

Pxx=pwelch(x,window,overlap)

Pxx=pwelch(x,window,overlap,nfft)

Pxx=pwelch(x,window,overlap,nfft,fs)

Pxx=pwelch(x,window,overlap,nfft,fs,range)

[Pxx,freq]=pwelch(...)

pwelch(...)

## Inputs

x
The signal.
Type: double
Dimension: vector | matrix
window
The window size, or the window vector.
Type: double | integer
Dimension: scalar | vector
overlap
The number of overlapping points in adjacent windows.
Type: integer
Dimension: scalar
nfft
The size of the fft.
Type: integer
Dimension: scalar
fs
The sampling frequency.
Type: double
Dimension: scalar
range
The spectrum type: 'onesided' or 'twosided'.
Type: string

## Outputs

Pxx
The power spectral density.
Type: vector
freq
The vector of frequencies corresponding to the density values.
Type: vector

## Example

n = 1000;
fs = 1125;
ts = 1/fs;
t = [0:1:(n-1)]*ts;
f1 = 24;
f2 = 56;
omega1 = 2 * pi * f1;
omega2 = 2 * pi * f2;
signal = 3 + 5 * cos(omega1 * t) + 7 * cos(omega2 * t);
window = hann(250,'periodic');
overlap = 125;
fftsize = 250;
range = 'onesided';
[Pxx,frq]=pwelch(signal,window,overlap,fftsize,fs,range);
f = frq([1, 2, 3, 4, 5, 13, 14, 15])'
p = Pxx([1, 2, 3, 4, 5, 13, 14, 15])'
f = [Matrix] 1 x 8
0  4  20  24  28  52  56  60
p = [Matrix] 1 x 8
1.50000  0.75000  0.52083  2.08333  0.52083  1.02083  4.08333  1.02083

With no return arguments, the function will automatically plot.

The 'onesided' output has a length of nfft/2+1 if nfft is even, or (nfft+1)/2 if nfft is odd.

It is often recommended to remove the signal mean prior to calling pwelch. The function does not remove the mean automatically.

The optional arguments can be defaulted using []. The defaults are as follows:
window
A Hamming window with the largest length that produces eight data segments.
overlap
One half of the window length.
nfft
The window length. A larger value will pad zeros to each block of windowed data.
fs
1.0 Hz.
range
'onesided'