void schmittS16LE (holharm_t * s, signed short int *indata, unsigned long count) { int i, j; float trigfact = 0.6; for (i = 0; i < count; i++) { *s->rn.schmittPointer++ = indata[i]; if (s->rn.schmittPointer - s->rn.schmittBuffer >= s->rn.blockSize) { int endpoint, startpoint, t1, t2, A1, A2, tc, schmittTriggered; s->rn.schmittPointer = s->rn.schmittBuffer; for (j = 0, A1 = 0, A2 = 0; j < s->rn.blockSize; j++) { if (s->rn.schmittBuffer[j] > 0 && A1 < s->rn.schmittBuffer[j]) A1 = s->rn.schmittBuffer[j]; if (s->rn.schmittBuffer[j] < 0 && A2 < -s->rn.schmittBuffer[j]) A2 = -s->rn.schmittBuffer[j]; } t1 = (int) (A1 * trigfact + 0.5); t2 = -(int) (A2 * trigfact + 0.5); startpoint = 0; for (j = 1; s->rn.schmittBuffer[j] <= t1 && j < s->rn.blockSize; j++); for (; !(s->rn.schmittBuffer[j] >= t2 && s->rn.schmittBuffer[j + 1] < t2) && j < s->rn.blockSize; j++); startpoint = j; schmittTriggered = 0; endpoint = startpoint + 1; for (j = startpoint, tc = 0; j < s->rn.blockSize; j++) { if (!schmittTriggered) { schmittTriggered = (s->rn.schmittBuffer[j] >= t1); } else if (s->rn.schmittBuffer[j] >= t2 && s->rn.schmittBuffer[j + 1] < t2) { endpoint = j; tc++; schmittTriggered = 0; } } if (endpoint > startpoint) { s->rn.afreq = (float) s->SAMPLE_RATE * (tc / (float) (endpoint - startpoint)); displayFrequency (s, s->rn.afreq); } } } };
void Recognize::schmittS16LE (signed short int *indata) { int i, j; for (i = 0; i < PERIOD; i++) { *schmittPointer++ = indata[i]; if (schmittPointer - schmittBuffer >= blockSize) { int endpoint, startpoint, t1, t2, A1, A2, tc, schmittTriggered; schmittPointer = schmittBuffer; for (j = 0, A1 = 0, A2 = 0; j < blockSize; j++) { if (schmittBuffer[j] > 0 && A1 < schmittBuffer[j]) A1 = schmittBuffer[j]; if (schmittBuffer[j] < 0 && A2 < -schmittBuffer[j]) A2 = -schmittBuffer[j]; } t1 = lrintf ((float)A1 * trigfact + 0.5f); t2 = -lrintf((float)A2 * trigfact + 0.5f); startpoint = 0; for (j = 1; schmittBuffer[j] <= t1 && j < blockSize; j++); for (; !(schmittBuffer[j] >= t2 && schmittBuffer[j + 1] < t2) && j < blockSize; j++); startpoint = j; schmittTriggered = 0; endpoint = startpoint + 1; for (j = startpoint, tc = 0; j < blockSize; j++) { if (!schmittTriggered) { schmittTriggered = (schmittBuffer[j] >= t1); } else if (schmittBuffer[j] >= t2 && schmittBuffer[j + 1] < t2) { endpoint = j; tc++; schmittTriggered = 0; } } if (endpoint > startpoint) { afreq = fSAMPLE_RATE *((float)tc / (float) (endpoint - startpoint)); displayFrequency (afreq); } } } };
static void insideDrawHandler(GL_PageControls_TypeDef* pThis, _Bool force, int relX, int relY) { // Setup impossible values (as much as possible static BandPreset previousBand = -1; static uint32_t previousFreq = -1; static uint32_t previousStep = -1; BandPreset curBand = FrequencyManager_GetSelectedBand(); uint32_t curFreq = FrequencyManager_GetCurrentFrequency(); uint32_t curStep = FrequencyManager_GetFrequencyStepSize(); // Redraw only when needed: _Bool redrawBand = force || curBand != previousBand; _Bool redrawFreq = force || curFreq != previousFreq || curStep != previousStep; // Draw the band if (redrawBand) { GL_SetFont(GL_FONTOPTION_8x12Bold); GL_SetTextColor(BIGBUTTON_COLOR_NORMAL_TEXT); GL_SetBackColor(BIGBUTTON_COLOR_NORMAL_BACK); int writeX = relX + OFFSETX_TEXT; int writeY = relY + OFFSETY_BAND; GL_PrintString(writeX, writeY, FrequencyManager_GetBandName(FrequencyManager_GetSelectedBand()), 0); previousBand = curBand; } // Draw the Frequency if (redrawFreq) { GL_SetFont(GL_FONTOPTION_8x12Bold); GL_SetTextColor(BIGBUTTON_COLOR_EDIT_TEXT); GL_SetBackColor(BIGBUTTON_COLOR_EDIT_BACK); int writeX = relX + OFFSETX_TEXT; int writeY = relY + OFFSETY_FREQ; displayFrequency(FrequencyManager_GetCurrentFrequency(), writeX, writeY, FrequencyManager_GetFrequencyStepSize()); previousFreq = curFreq; previousStep = curStep; } }
static void fftMeasure (int nframes, int overlap, float *indata) { #ifdef USE_FFTW int i, stepSize = fftSize/overlap; double freqPerBin = rate/(double)fftSize, phaseDifference = 2.*M_PI*(double)stepSize/(double)fftSize; if (!fftSample) fftSample = fftSampleBuffer + (fftSize-stepSize); // bzero(fftGraphData.db, GRAPH_MAX_FREQ); for (i=0; i<nframes; i++) { *fftSample++ = indata[i]; if (fftSample-fftSampleBuffer >= fftSize) { int k; Peak peaks[MAX_PEAKS]; for (k=0; k<MAX_PEAKS; k++) { peaks[k].db = -200.; peaks[k].freq = 0.; } fftSample = fftSampleBuffer + (fftSize-stepSize); for (k=0; k<fftSize; k++) { double window = -.5*cos(2.*M_PI*(double)k/(double)fftSize)+.5; fftIn[k] = fftSampleBuffer[k] * window; } fftwf_execute(fftPlan); for (k=0; k<=fftSize/2; k++) { long qpd; float real = creal(fftOut[k]), imag = cimag(fftOut[k]), magnitude = 20.*log10(2.*sqrt(real*real + imag*imag)/fftSize), phase = atan2(imag, real), tmp, freq; /* compute phase difference */ tmp = phase - fftLastPhase[k]; fftLastPhase[k] = phase; /* subtract expected phase difference */ tmp -= (double)k*phaseDifference; /* map delta phase into +/- Pi interval */ qpd = tmp / M_PI; if (qpd >= 0) qpd += qpd&1; else qpd -= qpd&1; tmp -= M_PI*(double)qpd; /* get deviation from bin frequency from the +/- Pi interval */ tmp = overlap*tmp/(2.*M_PI); /* compute the k-th partials' true frequency */ freq = (double)k*freqPerBin + tmp*freqPerBin; #ifdef USE_GRAPH int fi = (int)round(freq); if (fi < GRAPH_MAX_FREQ) { fftGraphData.db[fi] = magnitude; if (magnitude > fftGraphData.dbMax) fftGraphData.dbMax = magnitude; if (magnitude < fftGraphData.dbMin) fftGraphData.dbMin = magnitude; } /* printf("%+8.3f % .5f %i\n", freq, fftGraphData.scale_freq, (int)round(freq * fftGraphData.scale_freq)); */ #endif if (freq > 0.0 && magnitude > peaks[0].db) { memmove(peaks+1, peaks, sizeof(Peak)*(MAX_PEAKS-1)); peaks[0].freq = freq; peaks[0].db = magnitude; } } fftFrameCount++; if (fftFrameCount > 0 && fftFrameCount % overlap == 0) { int l, maxharm = 0; k = 0; for (l=1; l<MAX_PEAKS && peaks[l].freq > 0.0; l++) { int harmonic; for (harmonic=5; harmonic>1; harmonic--) { if (peaks[0].freq / peaks[l].freq < harmonic+.02 && peaks[0].freq / peaks[l].freq > harmonic-.02) { if (harmonic > maxharm && peaks[0].db < peaks[l].db/2) { maxharm = harmonic; k = l; } } } //displayFrequency(&peaks[l], lblFreq[l]); } displayFrequency(&peaks[k]); } memmove(fftSampleBuffer, fftSampleBuffer+stepSize, (fftSize-stepSize)*sizeof(float)); } } #endif #ifdef USE_DJBFFT float sample = *indata; fftr4_4(&sample); printf("f % 8.3f\n", sample); #endif }
int rxtx() { int s; // read ptt on PORTD int c = PIND; // listen reverse? if (rv) { if (c & (1<<REVERSE)) { lastFreq = 0; rv = FALSE; } } else { if (!(c & (1<<REVERSE))) { lastFreq = 0; rv = TRUE; } } if (tx) { //keep smeter clear s = 0; // switch from tx to rx?? if (c & (1<<PTT) ) { cbi(PORTC, TXON); lastFreq = 0; tx = FALSE; } } else { s = readRSSI(); displaySmeter(s); // switch from rx to tx? if (!(c & (1<<PTT) )) { // clear smeter s = 0; displaySmeter(s); sbi(PORTC, MUTE); sbi(PORTC, TXON); // force update pll lastFreq = 0; tx = TRUE; } } // calc value for ISR toneCount = 5*F_CPU/tone; // freq change or update needed? if (freq != lastFreq) { long int f = freq; if (tx) { f += (long int) shift*1000; setFrequency(f); } else { if (rv) f += (long int)shift*1000; setFrequency(f - IF); } displayFrequency(f); lastFreq = freq; lcdCursor(15,0); if (tx) lcdChar('T'); else lcdChar('R'); } return s; }