I’m currently working on this project that implies some DSP skills.
I must extract the audio from a movie and then, by analyzing it, I must determine when someone speaks or not, more like an voice activity detector.
I’m writing the code in Java (yes, I know it’s not the best choice) and only use a library to extract the audio from the video and JLayer so I can process an MP3.
My class that extracts the audio samples gets the samples consecutively for each channel, in my case two: LEFT0, RIGHT0, LEFT1, RIGHT1, LEFT2, RIGHT2, etc.
So this is what I’ve done so far:
- I put the samples for each channel in an array.
- I apply a Hamming window [N = 8192]:
double w = 0.54 - 0.46 * (Math.cos(2*Math.PI*buffer[i]/buffer.length-1));
fftBuffer[i] = new Complex(w, 0); - I then perform a simple FFT on each channel and then compute the magnitude
mag = re^2 + im^2;after that, I do a log scale (dB):mag_dB = 10 * log10(abs(mag));
Because I am “looking for voice” here, I need frequencies between 80 and 1000 (even tough the voice ranges between 80 Hz and 255 Hz). So, from the FFT I get a mirrored N = 8129 array from witch I need only the first N/2.
The frequency per bin (slot in the array resulted from the FFT) would be the sample rate (48.000 kHz)
/ N; that would be 48000 / 8192 = 5 Hz per bin. So I only look in the array at the values from FFT_Result[15] to FFT_Result[199] (16 * 5Hz = 80 Hz; 200 * 5 = 1000 Hz) right?!
I took a look on the frequency analyzer in Cool Edit Pro and all the amplitudes are negative. In my case, the first ones (the sound is in the background and isn’t loud) are negative, and after that, they are all positive. Aren’t they supposed to be negative? Am I missing out something over here?
So far, based on what I’ve remarked by looking at the frequency analyzer and phase analyzer in Cool Edit Pro, I need a threshold on this frequency range, some kind of algorithm to determine over a period of n milliseconds if the magnitude is constant over that frequency range and determine if the sound is centered. The last one must be done (I think) analyzing the phase angle, when someone speaks, the sound is always centered.
I didn’t manage to find a way to do that and I’m all confused with what I’ve done so far because I do not know if what I’ve done so far is right.
So, if you read all this, thank you for your patience and my questions are:
– have I done right what I’ve done so far?
– does the amplitude has to be negative?
– does anyone know how I can compute the phase for a number of samples?
In dB, the amplitude can be negative or positive, it doesn’t matter. What matters, is the value relative to some threshold. I would base the threshold on surrounding samples. Because the energy in spoken words goes up and down as syllables are spoken, a simple average (multiplied by some arbitrary factor you’ll have to play with to find what works well) would work fine as a threshold.
For phase in the time domain, you can first take a Hilbert transform, and then use atan2 on the real and imaginary parts of each sample to estimate instantaneous phase.