![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/600800/image.png)
How to align the power plot correctly?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Mathieu NOE
le 29 Avr 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)
![](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');
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Multirate Signal Processing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!