How do I calculate power for each signal?

35 ビュー (過去 30 日間)
Giggs B.
Giggs B. 2021 年 4 月 28 日
編集済み: Giggs B. 2021 年 4 月 29 日
So, I have an audio signal with fs=8192 and total samples=73113, which gives the total duration of the signal as 8.9249 seconds. My aim is to calculate the power of this signal for each second and plot it. Basically what I mean is to calculate the power of signal between 0th sec and 1st second and so on and then plot them with time on x axis. I tried calculating power element wise but I am not getting element wise power using "power=rms(y_b).^2", I am not getting correct vector so I am unable to move forward, I cannot think of a way, can someone plese help.
[y,fs]=audioread('h.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);
% how to calculate power for each signal?
%....code....
subplot (2,1,2);
plot(time1,power);
Thank you

採用された回答

Star Strider
Star Strider 2021 年 4 月 28 日
編集済み: Star Strider 2021 年 4 月 28 日
Use the buffer function to segment the signal into 1-second increments. Since you want 1-second samples, the vector length is simply ‘Fs’, so —
y = randn(73113,1); % Create Signal To Test Code (One Channel)
fs = 8192; % Sampling Frequency (Also Frame Length)
N = size(y,1); % Signal Length
time = linspace(0, N, N-1)/fs; % Time Vector
y_b = bandpass(y,[300 2000],fs); % Filter Signal
y_seg = buffer(y_b,fs); % Matrix Of Signal Segments
y_pwr = sum(y_seg.^2); % Calculate Power
figure
for k = 1:numel(y_pwr)-1
tv = time((1:fs)+fs*(k-1));
subplot(5,2,k)
plot(tv,y_seg(:,k))
grid
title(sprintf('Segment %d Power %.1f',k,y_pwr(k)))
end
subplot(5,2,k+1)
tv = time(fs*k:end);
plot(tv,y_seg(1:numel(tv),k+1))
grid
title(sprintf('Segment %d Power %.1f',k+1,y_pwr(k+1)))
EDIT — (28 Apr 2021 at 12:40)
Corrected typoographical error.
  5 件のコメント
Star Strider
Star Strider 2021 年 4 月 29 日
Since I am not certain how your code plotted them, the easy solution is to use xticklabels to change the labels. Alternatively, a stairs or bar plot (either of which would likely be more representative, anyway) could work, perhaps in addition to the xticklables change.
Giggs B.
Giggs B. 2021 年 4 月 29 日
編集済み: Giggs B. 2021 年 4 月 29 日
EDIT: Added new thread instead (since it was getting messy here): How to align the power plot correctly? - MATLAB Answers - MATLAB Central (mathworks.com)

サインインしてコメントする。

その他の回答 (1 件)

Mathieu NOE
Mathieu NOE 2021 年 4 月 28 日
hello
if you want to compute a running rms value , you can use the movstd function - see help movstd
and use K = fs=8192 to average the computation over 1 second interval;
movstd Moving standard deviation value.
Y = movstd(X,K) for a vector X and positive integer scalar K computes a
centered moving standard deviation by sliding a window of length K
along X. Each element of Y is the local standard deviation of the
corresponding values of X inside the window, with Y the same size as X.
When K is even, the window is centered about the current and previous
elements of X. The sliding window is truncated at the endpoints where
there are fewer than K elements from X to fill the window.
now this will give you a rms calculation with the same time increment as your data, you can decide afterwards to plot only values with 1 second interval (so index values multiple of fs = 8192 )
  1 件のコメント
Giggs B.
Giggs B. 2021 年 4 月 29 日
Thank you, I checked movstd, that also works!!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpectral Measurements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by