I am writing some code for audio analysis, and have currently got two signals with FFT performed on them.
I get the phase of my complex array by using:
ang = Math.atan(complex.im/complex.re)*180.0/Math.PI;
I then 'format' for wrapped phase:
if(complex.re < 0.0 && complex.im == 0.0){ang = 180.0;}
else if(complex.re < 0.0 && complex.im == -0.0){
ang = -180.0;}
else if(complex.re < 0.0 && complex.im > 0.0){
ang += 180.0;}
else if(complex.re < 0.0 && complex.im < 0.0){
ang += -180.0;}
phase.add(ang);
}
As I have converted this to degrees already, I then subtract channel 1 phase from channel 2 phase to give me the phase difference:
double phaseDiff = (((double)chan1phase.get(i)) - ((double)chan2phase.get(i)));
Finally, I add a method for keeping wrapped phase whilst subtracting the two phases:
if (phaseDiff < -180){
phaseDiff += 360;
}
if (phaseDiff > 180){
phaseDiff -= 360;
}
This works really well currently, however there are too many data points for my application to respond quickly. I am working with 16k or 32k FFT size, and that's a lot of data to plot and redraw quickly when I manipulate my data. I also occasionally get one or two data points throughout the whole plot which are noisy or differ from the expected path of travel, which I would like to get rid off.
I have already used a smoothing function to display my magnitude vs frequency, and this responds really quickly. However I am not confused on how best to smooth phase. I need to reduce the data points to approximately 1000 points over the audible frequency range (20Hz - 20kHz).
However I cannot just use the same method as I used for magnitude because of the wrapped phase. Would I need to average the data points within an octave (or preset band) prior to wrapping the phase?
Or am I better disregarding some points and interpolating the data?
Any guidance would be appreciated. Apologies if there is too much actual code here, I realise this is a DSP site and not programming site, but thought it useful to see exactly what I am doing (or where I have gone wrong!).