MATLAB Examples

# Example 8.2.5.b. Barlett'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; K = [1 4 8 16] ; % Number of Bartlett 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.15 Spectrum Estimation of two Sinusoids in White Gaussian Noise',... 'Visible','off','Position', [100 0 800 800]); for m=1:length(K) 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,:) = bart(x,K(m),PerLength); % Periodogram calculation using Barlett'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),' Bartlett periodograms using K = ', ... num2str(K(m)),' with L = ',num2str(N/K(m))]) end grid on; axis tight; ylim([-10 32]) % 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),' Bartlett periodograms' ]) axis tight; % set(gca,'YTick',-5:5:30); % xlabel('Frequency (units of pi)'); ylim([-10 32]) grid on; end % Restore the visibility of the figure. set(h1, 'Visible','on'); ```