%% Welch Estimate Using Specified Segment Length
% Obtain the Welch PSD estimate of an input signal consisting of a discrete-time sinusoid with an angular frequency of \pi/3 rad/sample with additive N(0,1) white noise.
% Create a sine wave with an angular frequency of \pi/3 rad/sample with additive N(0,1) white noise. Reset the random number generator for reproducible results. The signal has 512 samples.

rng default

n = 0:511;
x = cos(pi/3*n)+randn(size(n));

%
% Obtain the Welch PSD estimate dividing the signal into segments 132 samples in length. The signal segments are multiplied by a Hamming window 132 samples in length. The number of overlapped samples is not specified, so it is set to 132/2 = 66. The DFT length is 256 points, yielding a frequency resolution of 2\pi/256 rad/sample. Because the signal is real-valued, the PSD estimate is one-sided and there are 256/2+1 = 129 points. Plot the PSD as a function of normalized frequency.

segmentLength = 132;
[pxx,w] = pwelch(x,segmentLength);

plot(w/pi,10*log10(pxx))
xlabel('\omega / \pi')

% Copyright 2015 The MathWorks, Inc.