diff --git a/ch32v/ch32v203-goertzel/adcgoertzel.c b/ch32v/ch32v203-goertzel/adcgoertzel.c index 0ec9ad9..8d5f3f7 100644 --- a/ch32v/ch32v203-goertzel/adcgoertzel.c +++ b/ch32v/ch32v203-goertzel/adcgoertzel.c @@ -75,7 +75,6 @@ int g_volume_pwm = 127; // 0 - 127 (100%) (but you can go over 100) (For when us #define SAMPLETIME 1 // 0: 1.5 cycles; 1: 7.5 cycles; 2: 13.5 cycles; (0 would go fastest and is important in single-ADC mode, but 1 seems slightly better in 2-ADC mode) - #ifdef ENABLE_OLED_SCOPE #define SH1107_128x128 #define SSD1306_RST_PIN PA3 @@ -111,7 +110,7 @@ volatile uint16_t adc_buffer[ADC_BUFFSIZE]; int32_t g_goertzel_phasor_r = 32768; int32_t g_goertzel_phasor_i = 0; - +int32_t g_attenuation_pow2 = 4; #if 1 // Very basic setup, for tuning to 880AM @@ -423,8 +422,8 @@ void DMA1_Channel1_IRQHandler( void ) int32_t zp = g_goertzelp_store; int32_t zp2 = g_goertzelp2_store; - int32_t rr = (((int64_t)(g_goertzel_coefficient ) * (int64_t)zp<<1)>>32) - (zp2); - int32_t ri = (((int64_t)(g_goertzel_coefficient_s) * (int64_t)zp<<1)>>32); + int32_t rr = (((int64_t)(g_goertzel_coefficient ) * (int64_t)zp<<1)>>(32+g_attenuation_pow2)) - (zp2>>g_attenuation_pow2); + int32_t ri = (((int64_t)(g_goertzel_coefficient_s) * (int64_t)zp<<1)>>(32+g_attenuation_pow2)); // Advanced the current goertzel advance // phasor = phasor * advance; @@ -451,8 +450,8 @@ void DMA1_Channel1_IRQHandler( void ) // Now, rotate rr, ri by that phasor. // To get it in line >> 15, but we also want to divide by 8 (>>3) because that makes the rest of the math easier. - temp = (g_goertzel_phasor_r * ri + g_goertzel_phasor_i * rr) >> (15+3); - rr = (g_goertzel_phasor_r * rr - g_goertzel_phasor_i * ri) >> (15+3); + temp = (g_goertzel_phasor_r * ri + g_goertzel_phasor_i * rr) >> (15); + rr = (g_goertzel_phasor_r * rr - g_goertzel_phasor_i * ri) >> (15); ri = temp; // rr, ri are now in the correct frame of reference. Continue computing. @@ -474,6 +473,8 @@ void DMA1_Channel1_IRQHandler( void ) // This performs a low-pass IIR without exploding intensity_average. intensity_average = intensity_average - (intensity_average>>12) + (intensity>>6); + if( ((int32_t)intensity_average) >= 1<<23 ) intensity_average = (1<<23)-1; + if( ((int32_t)intensity_average) < 2048 ) intensity_average = 2048; #ifdef PWM_OUTPUT intensity = intensity * g_volume_pwm * g_pwm_period / (intensity_average>>(10-12)); @@ -767,7 +768,11 @@ int HandleHidUserGetReportSetup( struct _USBState * ctx, tusb_control_request_t int samps_to_send = (qibaselogs_head - last_baselog + LOG_GOERTZEL_LIST * 2 - 1) & (LOG_GOERTZEL_LIST-1); if( samps_to_send > 120 ) samps_to_send = 120; - ((uint32_t*)scratchpad)[0] = (intensity_average<<12) | samps_to_send; + int intensity_send = intensity_average; + + if( intensity_send >= (1<<24) ) + intensity_send = (1<<24)-1; + ((uint32_t*)scratchpad)[0] = (((uint32_t)intensity_send)<<8) | samps_to_send; ((uint32_t*)scratchpad)[1] = (g_lastper<<16) | g_lastlen; ((uint32_t*)scratchpad)[2] = (0<<16) | (((g_pwm_period+1)*g_goertzel_buffer)); //LSW = 144MHz / X @@ -835,6 +840,7 @@ void HandleHidUserReportOutComplete( struct _USBState * ctx ) ADC2->CTLR1 &= (~ADC_BUFEN); } } + if( numconfigs > 9) g_attenuation_pow2 = configs[11]; // Need to reset so we don't blast by. g_goertzel_samples = 0; diff --git a/ch32v/lib/calculator.html b/ch32v/lib/calculator.html index 420d29c..46f47fa 100644 --- a/ch32v/lib/calculator.html +++ b/ch32v/lib/calculator.html @@ -52,6 +52,7 @@ function SendGoertz() let tau = 3.1415926535*2.0; let omega = fr * tau; var textarea = document.getElementById("goertzeloutput"); + var g_attenuation_pow2 = Number( document.getElementById( "g_attenuation_pow2" ).value ); var goertzel_omega_per_sample_real = ( omega*2*(1<<29)); var g_goertzel_omega_per_sample = Math.round( goertzel_omega_per_sample_real ); @@ -64,21 +65,27 @@ function SendGoertz() var g_goertzel_advance_i = Math.sin( goertzel_phasor_advance_radians_per_sample ) * 32768; var g_exactcompute = exact_compute; - textarea.value = - "int g_pwm_period = ("+n+"-1); // " + system_rate/lastGn/1000000. + " MHz Samplerate\n" + - "int g_exactcompute = ("+exact_compute+");\n" + - "int g_goertzel_buffer = ("+brf+");\n" + - "int32_t g_goertzel_omega_per_sample = " + g_goertzel_coefficient.toFixed(0) + "; // " + ( omega / (3.1415926535*2.0)).toFixed(6) + " of whole per step / " + mhz.toFixed(6) + "MHz\n" + - "int32_t g_goertzel_coefficient = " + g_goertzel_coefficient.toFixed(0) + ";\n" + - "int32_t g_goertzel_coefficient_s = " + g_goertzel_coefficient_s.toFixed(0) + ";\n" + - "int32_t g_goertzel_advance_r = " + g_goertzel_advance_r.toFixed(0) + ";\n" + - "int32_t g_goertzel_advance_i = " + g_goertzel_advance_i.toFixed(0) + ";\n"; - // Highlight its content - textarea.select(); + if( textarea ) + { + textarea.value = + "int g_pwm_period = ("+n+"-1); // " + system_rate/lastGn/1000000. + " MHz Samplerate\n" + + "int g_exactcompute = ("+exact_compute+");\n" + + "int g_goertzel_buffer = ("+brf+");\n" + + "int32_t g_goertzel_omega_per_sample = " + g_goertzel_coefficient.toFixed(0) + "; // " + ( omega / (3.1415926535*2.0)).toFixed(6) + " of whole per step / " + mhz.toFixed(6) + "MHz\n" + + "int32_t g_goertzel_coefficient = " + g_goertzel_coefficient.toFixed(0) + ";\n" + + "int32_t g_goertzel_coefficient_s = " + g_goertzel_coefficient_s.toFixed(0) + ";\n" + + "int32_t g_goertzel_advance_r = " + g_goertzel_advance_r.toFixed(0) + ";\n" + + "int32_t g_goertzel_advance_i = " + g_goertzel_advance_i.toFixed(0) + ";\n"; + "int32_t g_attenuation_pow2 = " + g_attenuation_pow2.toFixed(0) + ";\n"; - // Copy the highlighted text - document.execCommand("copy"); + // Highlight its content + textarea.select(); + + // Copy the highlighted text + document.execCommand("copy"); + + } updateWebHidDeviceWithParameters( [ (n-1)|0, @@ -90,6 +97,7 @@ function SendGoertz() g_goertzel_advance_r|0, g_goertzel_advance_i|0, document.getElementById( "toggle_adc_buffer").checked ? 1 : 0, + g_attenuation_pow2, ] ); // Update toggle control @@ -109,6 +117,17 @@ function toggleBuffer( ths ) SendGoertz(); } +function attenuationpow2wheel( event, ths ) +{ + event.preventDefault(); + let dy = event.deltaY > 0 ? -1 : 1; + var thss = Number(ths.value) + dy; + if( thss < -9 ) thss = -9; + if( thss > 9 ) thss = 9; + ths.value = thss; + UpdateQuickSettings(); +} + function mhzm( event, ths ) { event.preventDefault(); @@ -126,8 +145,11 @@ function mhzm( event, ths ) lastGmhz = hz/10000000.0; document.getElementById("targetmhz").value = lastGmhz; + UpdateQuickSettings(); +} - +function UpdateQuickSettings() +{ let xtal = Number(document.getElementById("crystalmhz").value ); let target = Number(document.getElementById("targetmhz").value ); let quadrature = document.getElementById("QUADRATURE").checked; @@ -317,16 +339,16 @@ function computeTable() // Add widget to control various things, realtime. contents += "
Scroll Wheel Control:
"; - contents += ""; - contents += ""; - contents += "."; - contents += ""; - contents += ""; - contents += ","; - contents += ""; - contents += ""; - contents += ","; - contents += ""; + contents += ""; + contents += ""; + contents += "."; + contents += ""; + contents += ""; + contents += ","; + contents += ""; + contents += ""; + contents += ","; + contents += ""; contents += "
"; contents += "v Click in this row; "; contents += ''; @@ -393,7 +415,7 @@ function computeTable() document.getElementById( "TABLE" ).innerHTML = contents; } -const savedFields = ["crystalmhz", "targetmhz", "quanta", "quantasearch", "toggle_adc_buffer"]; +const savedFields = ["crystalmhz", "targetmhz", "quanta", "quantasearch", "toggle_adc_buffer", "g_attenuation_pow2"]; function SaveDefaults() { @@ -476,7 +498,10 @@ Live Control:
d\\h
+
+
Pow2 Attenuation: +
diff --git a/ch32v/lib/webhidcontrol.js b/ch32v/lib/webhidcontrol.js index b6297bb..dc8053b 100644 --- a/ch32v/lib/webhidcontrol.js +++ b/ch32v/lib/webhidcontrol.js @@ -329,8 +329,9 @@ async function sendLoop() document.getElementById( "StatusPerf" ).innerHTML = (kBsecAvg).toFixed(2) + " kBytes/s
" + - (xActionSecAvg).toFixed(2) + "transactions/sec
" + - "Count: " + goodCount + " / " + badCount + "
Inten: " + ((Math.log( lastIntensity * lastIntensity )/Math.log(10)) * 10-130).toFixed(2) + "db"; + (xActionSecAvg).toFixed(2) + "transactions/sec
"; + document.getElementById( "GeneralData" ).innerHTML = + "Count: " + goodCount + " / " + badCount + "
Inten: " + ((Math.log( lastIntensity * lastIntensity )/Math.log(10)) * 10-120).toFixed(2) + "db (" + lastIntensity + ")"; lastTime = thisTime; } else if( frameNo % updateStatsPerfPer == 2 ) @@ -356,8 +357,7 @@ async function sendLoop() ctx.fillStyle = `rgb( 255 255 255 )`; - let mulcoeff = 30000.0 / lastIntensity; - + let mulcoeff = 2500.0 / lastIntensity; var lot = 1.2; var x = 253; for( var i = (IQHistoryHead-1) & (IQHistoryLen-1); i != IQHistoryHead|0; i = (i - 1 + IQHistoryLen) & (IQHistoryLen-1) ) @@ -412,7 +412,7 @@ async function sendLoop() let time_used = data[1]&0xffff; let sample_divisor = data[2]&0xffff; let demodbuffer = new Float32Array(numq); - let mulcoeff = 100.0 / lastIntensity; + let mulcoeff = 16.0 / lastIntensity; for( var i = 0|0; i < numq; i++ ) { let vv = IQHistoryArray[IQHistoryHead] = data[i+3];