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

rng default

n = 0:319;
x = cos(pi/4*n) + randn(size(n));

%
% Obtain the Welch PSD estimate dividing the signal into segments 100 samples in length. Use the default overlap of 50%. Specify the DFT length to be 640 points so that the frequency of \pi/4 rad/sample corresponds to a DFT bin (bin 81). Because the signal is real-valued, the PSD estimate is one-sided and there are 640/2+1 points.

segmentLength = 100;
nfft = 640;
pxx = pwelch(x,segmentLength,[],nfft);

plot(10*log10(pxx))
xlabel('rad/sample')
ylabel('dB / (rad/sample)')

% Copyright 2012 The MathWorks, Inc.