Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing a thesis that involves making an app that listens to background noise and detects high energy dog barks.

I have used the following code to store samples of the wav file into an array buffer.



public static void main(String[] args) throws IOException {
File file = new File("dogtest2.wav");

ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

int read;

byte buff[];
buff = new byte[1024];

while ((read = in.read(buff)) > 0)
{
out.write(buff, 0, read);
}
out.flush();
byte[] audioBytes = out.toByteArray();
}
}



Now I want to decode the buffer samples and find the RMS (Average) value of all the samples, so anything above this can be tagged as an unusual acoustic event.

What I have tried:

I have researched this continuously online and my supervisor told me I will need to use RMS energy to analyse the audio frames from the wave file for an average energy. So far In have not find anything that is relevant to my situation (as far as I can tell ).


Any guidance about what I should be reading up on or any other advice would be greatly appreciated.
Posted
Updated 12-May-19 23:00pm

Play the file through a pair of speakers and measure the voltage and amperage.

Measuring amplifier output power[^]

https://itstillworks.com/determine-speaker-rms-12106808.html[^]
 
Share this answer
 
Well, you'd just do it the same way you'd do it with any other language. Namely, you calculate the square of the magnitude of each sample, add these squares all together, divide by the number of samples and then take the square-root of it.

To say it another way, this code computes this value for code running in a web-browser.

JavaScript
function calcRMS(signal)
{
	var numSamples = signal.length;
	var total = 0;
	for (var cur=0; cur<numSamples; cur++)
	{
		total += signal[cur]*signal[cur];
	}
	var result = Math.sqrt(total / numSamples);
	return result;
}


Since it takes the same energy to pull the microphone-diaphram towards you by a micrometer, as it takes it to push it away from you, a system is needed whereby a sample of -0.5 contributes just as much to the energy computation as a sample of 0.5

By taking the square of each sample, we eliminate the negative sign. This allows us to compute the same value for energy regardless of which direction the microphone is deflected in.
E.g
-2*-2 = 4.
and
2*2 = 4
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900