MATLAB Examples

Example 8.2.6. Welch's Method - Two Simusoids in White Noise.

Workspace Initialization.

```clc; clear; close all; ```

Signal Definition

```A1 = sqrt(10); A2 = 1; w1 = 0.2*pi; w2 = 0.25*pi; N = 512; L = [512 256 128 64] ; % Length of data sections (without overlap). over = 0.5; % Overlap between successive data sections. NumOfRuns = 50; PerLength = 4*1024; % This is the fft length used to calculate the periodogram. freq = 0:2/(PerLength-1):2; h1 = figure('NumberTitle', 'off','Name', ... 'Figure 8.17 Spectrum Estimation of two Sinusoids in white noise Using Welch''s method', ... 'Visible','off','Position', [100 0 800 850]); for m=1:length(L) Px = zeros(NumOfRuns,PerLength); for k=1:NumOfRuns n = 0:N-1; noise = randn(1,N); phi1 = 2*pi*(rand() - 0.5); phi2 = 2*pi*(rand() - 0.5); x = A1*sin(n*w1 + phi1) + A2*sin(n*w2 + phi2) + noise; Px(k,:) = welch(x,L(m),over,2,PerLength); % Periodogram calculation using Welch's method. end subplot(4,2,2*m-1) plot(freq(1:PerLength/2),10*log10(Px(:,1:PerLength/2)),'k','LineWidth',0.1) if m==1 title(['Overlay plot of ', num2str(NumOfRuns),' periodograms using N = 512 data samples']) else title(['Overlay plot of ', num2str(NumOfRuns),' Welch periodograms using L = ', ... num2str(L(m)), ' with ',num2str(100*over),' % overlap']) end grid on; axis tight; xlabel('Frequency (units of pi)'); subplot(4,2,2*m) plot(freq(1:PerLength/2),1/NumOfRuns*sum(10*log10(Px(:,1:PerLength/2))),'r','LineWidth',1) title(['Ensemble Average of ',num2str(NumOfRuns),' Welch periodograms' ]) axis tight; xlabel('Frequency (units of pi)'); grid on; end % Restore the visibility of the figure. set(h1, 'Visible','on'); ```