MATLAB Answers

# How do I calculate power for each signal?

1 view (last 30 days)
Giggs B. on 28 Apr 2021
Edited: Giggs B. on 29 Apr 2021
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
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Star Strider on 28 Apr 2021
Edited: Star Strider on 28 Apr 2021
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 CommentsShowHide 4 older comments
Giggs B. on 29 Apr 2021
EDIT: Added new thread instead (since it was getting messy here): How to align the power plot correctly? - MATLAB Answers - MATLAB Central (mathworks.com)

Sign in to comment.

### More Answers (1)

Mathieu NOE on 28 Apr 2021
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 CommentShowHide None
Giggs B. on 29 Apr 2021
Thank you, I checked movstd, that also works!!

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!