views:

46

answers:

2

Hi guys,

I'm trying to write an .m file to extract energy features from an audio track but I seem to be having trouble in its implementation:

% Formula for calculating RMS

[f, fs, nb] = wavread('Three.wav');

frameWidth=441; %10ms
numSamples=length(x);
numFrames=(numSamples/1);
energy(frame)=0;

for frame=1:numFrames,
    startSample=(frame-1)*frameWidth+1;
    endSample=startSample+frameWidth-1;
% Calculate frame energy
    for i=startSample:endSample
        energy(frame)=energy(frame)+x(i)^2;
    end
end

I run that file in MATLAB and get the following error:

??? Attempted to access x(2); index out of bounds because numel(x)=1. Error in ==> myrms at 12 energy(frame)=energy(frame)+x(i)^2;

Any help would be much appreciated.

+2  A: 

You should be using f instead of x, since f is the actual signal loaded from your .wav file. The variable x was probably just some other scalar in your workspace, which is why you were getting the error you saw.

There are a few other corrections/improvements that should be made to your code. First, as Paul R pointed out, you need to correct how you compute numFrames. Second, energy should be initialized as a vector of zeroes. Third, you can reduce the inner for loop to a one-line vectorized operation.

Here's how I would rewrite your code (EDIT: Based on comments, I have updated the code to save a few extra variables computed in the loop):

[y, fs, nb] = wavread('Three.wav');  %# Load the signal into variable y

frameWidth = 441;                          %# 10 msec
numSamples = length(y);                    %# Number of samples in y
numFrames = floor(numSamples/frameWidth);  %# Number of full frames in y
energy = zeros(1,numFrames);               %# Initialize energy
startSample = zeros(1,numFrames);          %# Initialize start indices of frame
endSample = zeros(1,numFrames);            %# Initialize end indices of frame

for frame = 1:numFrames                              %# Loop over frames
  startSample(frame) = (frame-1)*frameWidth+1;       %# Starting index of frame
  endSample(frame) = frame*frameWidth;               %# Ending index of frame
  frameIndex = startSample(frame):endSample(frame);  %# Indices of frame samples
  energy(frame) = sum(y(frameIndex).^2);             %# Calculate frame energy
end
gnovice
Brilliant.Just noticed the mistake I made in the initial post too, I shouldn't have used f at all, I didn't even have it written down anywhere on the piece of paper where I was writing the initial code. How embarrassing.Thank you for the help anyway, it's much appreciated.
Velocity
I have no idea whether you will get a notification that I have replied to this but here goes anyway.Is there a way to apply a label or tag to each frame that is created? Even if it is as basic as "Frame 1""Frame 2" etc.I want to perform a few functions myself to find the areas of the song that I want to extract, but I have no idea how to recall the frames once I have that data.For instance if I perform auto-correlation and then peak detection functions, I then want to map that frame back to its specific position within the song so that I can then extract the thumbnail.
Velocity
@Velocity: One way to do that is to save the values of `startSample` and `endSample` that you compute in your loop. I've updated the code in my answer to show how to do this.
gnovice
A: 

Shouldn't this line:

numFrames=(numSamples/1);

be something like:

numFrames=(numSamples / frameWidth);

or possibly:

numFrames=((numSamples + frameWidth - 1) / frameWidth);

?

Paul R
I originally had:numFrames=flux(numSamples/numFrames)but MATLAB didn't like that either.
Velocity
A call to [FLOOR](http://www.mathworks.com/access/helpdesk/help/techdoc/ref/floor.html) may also be needed in case `numFrames` doesn't come out as an exact integer.
gnovice
Yes, total idiocy on my part. I couldn't read my own scrawly handwriting. FLOOR is what I wanted, not flux.
Velocity