How would I plot signal energy of a .wav file making sure that time is the value of the x-axis?

43 views (last 30 days)
When using the audioread function to read a .wav file, how would I plot the signal's energy making sure that the time domain is the value of the x-axis? This example was referenced but isn't working out. Assuming that if the .wav file is 30 seconds long, I wouldn't want the time domain to be t=0:0.01:1; considering that would only graph 1 sec of the 30 sec recording.
% time domain
t=0:0.01:1;
% signal x
x=sin(2*pi*t);
% signal's Energy
E=x.^2;
% plots
plotyy(t,x,t,E), grid on, axis([0 max(t) -2 2]) xlabel('t'), ylabel('x, E')
title('Signal and Energy')

Answers (2)

Star Strider
Star Strider on 30 Apr 2023
The audioread function returns the signal amplitude matrices and the sampling frequency. To create a time vector for the ‘y’ data, use the linspace function with the values derived from the returned data.
Try this —
t=0:0.01:1;
x=sin(2*pi*t);
audiowrite('test.wav',x,1/t(2))
figure
plot(t,x)
grid
title('Input')
[y,Fs] = audioread('test.wav');
L = size(y,1);
t = linspace(0, L-1, L)/Fs;
figure
plot(t, y, 'DisplayName','Energy = y')
hold on
plot(t, y.^2, 'DisplayName','Power = y^2')
hold off
grid
legend('Location','best')
title('Output')
.
  2 Comments
Andrew
Andrew on 3 May 2023
Do you have to use sin(2*pi*t) in order to plot the energy of a signal? Or could you use an fft function?
Star Strider
Star Strider on 3 May 2023
THe ‘sin(2*pi*t)’ here is the signalk that you defined in your code, so I used it. Measuring the contents of the signal recovered from the .wav file will be appropriate to that signal. The Fourier transform (fft) is another way of measuring the signal characteristics. Parseval’s theorem indicates that the results will be the same in the time and frequency domain.

Sign in to comment.


Paul
Paul on 30 Apr 2023
Hi Andrew,
The energy in a signal is a single number, so it's not clear how one would plot that.
The instantaneous power is a function of time, so we can plot that.
load handel.mat
filename = 'handel.wav';
audiowrite(filename,y,Fs);
clear y Fs
[y,Fs] = audioread('handel.wav');
Assuming that the discrete time signal, y, is zero, for n <= 0 and n > numel(y) the instanteous power is
ipower = abs(y).^2;
plot(1:numel(y),ipower)
and the energy is
E = sum(ipower)
E = 2.8153e+03
If we want to treat the elements of y as samples of a continuous time signal, then the energy in the continuous time signal is approximated by
Ts = 1/Fs;
E = sum(ipower)*Ts
E = 0.3437
Here is a link to one Reference for these definitions.
Here is link to an Answer that may be of interest.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!