MATLAB Examples

Example 8.2.2 Periodogram of a Sinusoid in Noise.

Contents

Workspace initialization.

clc; clear; close all;

Signal Definition

A = 5;
w0 = 0.4*pi;
N = [64 128 256 512 1024 2048]; % Number of data points used.

NumOfRuns = 50;
PerLength = 4096;
freq = 0:2/(PerLength-1):2;

Creation of First Figure.

h1 = figure('NumberTitle', 'off','Name', ...
                   'Figure 8.6 (1 of 2) The Periodogram of a Sinusoid in White Noise', ...
                   'Visible','off','Position', [430 110 800 700]);

for m=1:length(N)/2

Calculate the Periodograms Using a Variable Number of Samples.

    Px = zeros(NumOfRuns,PerLength);
    for k=1:NumOfRuns
         n = 0:N(m)-1;
         noise = randn(1,N(m));
         phi = 2*pi*(rand() - 0.5);
         x = A*sin(n*w0 + phi) + 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 40])
    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([-10 40])
    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.6 (2 of 2) The Periodogram of a Sinusoid in White Noise',...
                   'Visible','off','Position', [430 110 800 700]);
set(0,'CurrentFigure',h2);

for m=length(N)/2+1:length(N)

Calculate the Periodograms Using a Variable Number of Samples.

    Px = zeros(NumOfRuns,PerLength);
    for k=1:NumOfRuns
         n = 0:N(m)-1;
         noise = randn(1,N(m));
         phi = 2*pi*(rand() - 0.5);
         x = A*sin(n*w0 + phi) + 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([-10 40])
    grid on;
end

% Restore the visibility of the second figure.
set(h2, 'Visible','on');