java - variable showing strange value when implementing NLMS algorithm in Android -
i have been scratching head day , have run out of ideas now, posting here
i trying implement feedback suppressor in android using nlms algorithm
use audiorecord obtain audio samples mic , audiotrack play back. read in 1 sample @ time short variable, convert double, pass through algorithm, convert short , send speaker. however, getting weird values in intermediate variable , canot understand why happening. here code:
public class mainactivity extends activity { audiomanager = null; audiorecord record =null; audiotrack track =null; final int sample_frequency = 16000; // original 44100 final int size_of_record_array = 1; // 1024 original; 1000 / 40 = 25 // final int wav_sample_multiplication_factor = 1; final double wav_sample_multiplication_factor = 0.5; final int n = 4; // original 40 final int feedback_delay_in_msec = 1; // use small integer values here keep calculation of no_of_delay_samples becoming non-whole number // final int no_of_delay_samples = sample_frequency / (feedback_delay_in_msec * 1000); // no_of_delay_samples = 16 final int no_of_delay_samples = 0; final int total_size_of_x = n + no_of_delay_samples; int = 0, n = 0; // n represents nth sample boolean isplaying = false; // represents if pass through button pressed boolean applydsp = false; // represents if apply filter button pressed boolean bufferfull = false; private volatile boolean keepthreadrunning; double[] w = new double[n]; // w represents filter coefficients double[] x = new double[total_size_of_x]; double e; double d; double send_out; double mu; double y = 0; // /* private randomaccessfile statefile; string statefileloc = environment.getexternalstoragedirectory().getpath(); filedescriptor fd; // */ class mythread extends thread{ private volatile boolean needstopassthrough; // /* mythread(){ super(); } mythread(boolean newptv){ this.needstopassthrough = newptv; } // */ // /* @override public void run(){ short[] lin = new short[size_of_record_array]; short[] speaker = new short[size_of_record_array]; double speaker_double; int num = 0; log.d("mylog", "entered run"); if(needstopassthrough){ record.startrecording(); track.play(); log.d("mylog", "comes here before btn press?"); } n = total_size_of_x -1; while (keepthreadrunning) { // thread runs until loop stops; loop runs long program running num = record.read(lin, 0, size_of_record_array); for(i=0;i<lin.length;i++) d = (double)lin[i]; // line requires lin[] has single element array if(isplaying){ if(applydsp){ y=0.0; // initialize every time for(i=0; i<n; i++){ y += w[n-1-i] * x[n-i - no_of_delay_samples]; } // implementing step 2 e = d - y; // /* try { statefile.writedouble(e); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } // implementing step 3 mu = 0.5 / (x[n] * x[n] + 0.01); // implementing step 4 for(i=0; i<n; i++){ w[n-1-i] = w[n-1-i] + 2.0*mu*e*x[n-i - no_of_delay_samples]; } } // closing of if(applydsp) block // implementing step 5 for(i=0;i<total_size_of_x-1;i++){ x[i] = x[i+1]; } send_out = e; speaker_double = send_out * wav_sample_multiplication_factor; // implementing step 6 x[total_size_of_x -1] = speaker_double; for(i=0;i<speaker.length; i++) speaker[i] = (short)speaker_double; track.write(speaker, 0, num); } // if(isplaying) block closed; represents if "pass through" button has been clicked } // while (keepthreadrunning) closes here; infinite loop runs long program running record.stop(); track.stop(); record.release(); track.release(); } public void stopthread(){ keepthreadrunning = false; } } // end of mythread class mythread newthread;
i have included part of code contains thread performs nlms keep short. since size_of_record_array
1, lin
variable in num = record.read(lin, 0, size_of_record_array);
short
array 1 element. (in fact, anu double or short array may encountered in above code have single element). line e = d - y
key here. d value mic (d short typecasted double). during every run of while (keepthreadrunning) {
infinite loop, new value e should calculated, later typecasted short , passed audiotrack. problem e
not getting proper values in case, , there no output speakers. if send variable d
audiotrack, whatever input spoken in microphone appears @ output (with slight delay, expected). if try write e
variable values file, getting strange values. since using string.valueof(e)
did not work out me (each string character considered 16-bit character , 0
appears <space>0
, -1
appears <space>-<space>1
), directly wrote double value file using randomaccessfile.write double , viewed file in hex viewer. seems there random value @ beginning of file, after pattern emerges, not sure why there. screenshot of hex file shown below:
the pattern shown continues till end of file. why happening? since there no output assumed @ least should have been 0, can seen figure it's not 0 either, instead 7f f8 00 00 00 00 00 00
repeated again , again. please me determine why happening.
ps: boolean values isplaying
, applydsp
represent state of 2 buttons have on interface. when press applydsp
button on interface, speaker makes short , relatively loud "pop" sound, assume may reason random values in beginning of file containing values of e
shown above, not sure this, , not sure abput why popping noise there in first place.
Comments
Post a Comment