MATLAB Answers

How to align the power plot correctly?

4 views (last 30 days)
Giggs B.
Giggs B. on 29 Apr 2021
Commented: Mathieu NOE on 30 Apr 2021
I am calculating power of signal for each second and then plotting it on the power plot graph. What I want: For example the power of signal between 0th and 1st second should be plotted on "1" of x-axis of power plot.
What I have: The plot is off by 1 unit. The power calculated for 0-1 sec is shown on "0" instead of "1". How can I align it with the power calculations?*
*I need my both graphs to start from 0. I want the axis of both the plots to be on the same line as shown in graph.
If you see the figure attached, the power of signal between 4th and 5th second is shown at "4" on x axis, instead it should be shown on "5" in power plot.
I have this code:
[y,fs]=audioread('Untitled_40.wav');
y_b=bandpass(y,[300 2000],fs);
N=length(y);
time=N/fs;
t = linspace(0, time, N);
subplot(2,1,1);
plot (t,y_b);
xlim([0 time])
ylim([-0.5 0.5])
xticks([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21]);
yticks([-0.5 -0.25 0 0.25 0.5])
grid on;
xlabel('Time');
ylabel('Amplitude');
title('Raw data');
y_seg=buffer(y_b,fs);
y_p=rms(y_seg).^2;
x=(0:time);
subplot (2,1,2);
plot(x,y_p);
xlim([0 time])
xticks([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21]);
grid on;
grid minor;
xlabel('Time');
ylabel('Power');
title('Power plot');

Accepted Answer

Mathieu NOE
Mathieu NOE on 29 Apr 2021
hello
for me it would make sense to add simply half the buffer lenght to your time vector in the second graph, so that the power graph is aligned to the middle of the raw data buffers
this is how it looks like (on another wav file)
code
[y,fs]=audioread('Untitled_40.wav');
y_b=bandpass(y,[300 2000],fs);
N=length(y);
time=N/fs;
t = linspace(0, time, N);
subplot(2,1,1);
plot (t,y_b);
xlim([0 time])
ylim([-0.5 0.5])
xticks([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21]);
yticks([-0.5 -0.25 0 0.25 0.5])
grid on;
xlabel('Time');
ylabel('Amplitude');
title('Raw data');
y_seg=buffer(y_b,fs);
y_p=rms(y_seg).^2;
x=(0:time)+0.5; % updated here
subplot (2,1,2);
plot(x,y_p);
xlim([0 time])
xticks([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21]);
grid on;
grid minor;
xlabel('Time');
ylabel('Power');
title('Power plot');

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!