MATLAB Examples

# Example 8.2.3 Periodogram Resolution.

## Workspace initialization.

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

## Signal Definition

```A1 = 5; A2 = 5; w1 = 0.4*pi; w2 = 0.45*pi; N = [40 64 128 256 512 1024]; NumOfRuns = 50; PerLength = 4096; % This is the fft length used to calculate the periodogram. freq = 0:2/(PerLength-1):2; ```

## Creation of First Figure.

```h1 = figure('NumberTitle', 'off','Name', ... 'Figure 8.8 (1 of 2) The Periodogram of two Sinusoids in White Noise', ... 'Visible','off','Position', [430 110 800 700]); for m=1:length(N)/2 ```
``` Px = zeros(NumOfRuns,PerLength); for k=1:NumOfRuns n = 0:N(m)-1; noise = randn(1,N(m)); 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,:) = per(x,PerLength); end ```

## Plot the Periodograms.

``` subplot(length(N)/2,2,2*m-1) plot(freq(1:PerLength/2),10*log10(Px(:,1:PerLength/2)),'k','LineWidth',0.1) title(['Overlay plot of ',num2str(NumOfRuns),' periodograms using ',num2str(N(m)),' data samples']) grid on; axis tight; ylim([-30 30]) xlabel('Frequency (units of pi)'); subplot(length(N)/2,2,2*m) plot(freq(1:PerLength/2),1/NumOfRuns*sum(10*log10(Px(:,1:PerLength/2))),'r','LineWidth',1) title(['Average of ',num2str(NumOfRuns),' periodograms using ',num2str(N(m)),' data samples']) axis tight; set(gca,'YTick',-5:5:35); xlabel('Frequency (units of pi)'); ylim([-5 30]) grid on; ```
```end % Restore the visibility of the first figure. set(h1, 'Visible','on'); ```

## Creation of Second Figure.

```h2 = figure('NumberTitle', 'off','Name', ... 'Figure 8.8 (2 of 2) The Periodogram of two Sinusoids in White Noise', ... 'Visible','off','Position', [430 110 800 700]); set(0,'CurrentFigure',h2); for m=length(N)/2+1:length(N) ```
``` Px = zeros(NumOfRuns,PerLength); for k=1:NumOfRuns n = 0:N(m)-1; noise = randn(1,N(m)); 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,:) = per(x,PerLength); end ```

## Plot the Periodograms.

``` subplot(length(N)/2,2,2*m-1-length(N)) plot(freq(1:PerLength/2),10*log10(Px(:,1:PerLength/2)),'k','LineWidth',0.1) title(['Overlay plot of ',num2str(NumOfRuns),' periodograms using ',num2str(N(m)),' data samples']) grid on; axis tight; ylim([-30 40]) xlabel('Frequency (units of pi)'); subplot(length(N)/2,2,2*m-length(N)) plot(freq(1:PerLength/2),1/NumOfRuns*sum(10*log10(Px(:,1:PerLength/2))),'r','LineWidth',1) title(['Average of ',num2str(NumOfRuns),' periodograms using ',num2str(N(m)),' data samples']) axis tight; set(gca,'YTick',-5:5:35); xlabel('Frequency (units of pi)'); ylim([-5 40]) grid on; ```
```end % Restore the visibility of the first figure. set(h2, 'Visible','on'); ```