![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/600800/image.png)
How to align the power plot correctly?
조회 수: 1 (최근 30일)
이전 댓글 표시
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.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/600570/image.bmp)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/600570/image.bmp)
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); %Edited the formula of power on 7/26/2021
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');
댓글 수: 0
채택된 답변
Mathieu NOE
2021년 4월 29일
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/600800/image.png)
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');
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!