# Hello! How to plot cumulative periodogram in Matlab with 95% confidence interval?

3 views (last 30 days)
Anna Grigoryeva on 17 May 2017
Answered: Dimitris Iliou on 19 May 2017
How to plot cumulative periodogram in Matlab with 95% confidence interval? Similar to one in R

Dimitris Iliou on 19 May 2017
If I understand correctly, you want to have a MATLAB script that resembles the behavior of 'cpgram' in R.
A possible answer to your question is to use the periodogram function that MATLAB provides, and then take the cumulative sum of the power.
I will use an example from that documentation page to illustrate my suggestion.
The periodogram can be calculated using the following:
% This is a documentation example
fs = 1000;
t = 0:0.001:1-0.001;
x = cos(2*pi*100*t)+sin(2*pi*150*t)+randn(size(t));
[pxx,f,pxxc] = periodogram(x,rectwin(length(x)),length(x),fs,...
'ConfidenceLevel', 0.95);
% Plotting
figure()
plot(f,10*log10(pxx))
hold on
plot(f,10*log10(pxxc),'r-.')
hold off
xlim([85 175])
xlabel('Hz')
ylabel('dB')
title('Periodogram with 95%-Confidence Bounds')
and the outcome of that will look something like this:
From the variables calculated by the periodogram function, you can use the pxx variable to calculate the cumulative sum of the power.
You can do this with the following code:
% Calculate the Cumulative Sum
cpxx = cumsum(pxx)./sum(pxx);
% Plotting
figure()
plot(f,cpxx)
hold on
% These two lines correspond to the 95% interval
plot(f,f./max(f)+0.05,'r--')
plot(f+0.05*max(f),f./max(f),'r--')
axis([0 500 0 1])
xlabel('Frequency(Hz)')
ylabel('Cumulative periodogram')
hold off
This code will yield the following result: