/* FFTFunctions for Nextion LCD and Control MCU This code is for FFT and CW Decode. KD8CEC, Ian Lee ----------------------------------------------------------------------- //The section on CW decode logic is specified at the bottom of this code. License : I follow the license of the previous code and I do not add any extra constraints. I hope that the Comment I made or the Comment of OZ1JHM will be maintained. **********************************************************************/ #include #include "TeensyDSP.h" // Code Referency : http://paulbourke.net/miscellaneous/dft/ // DFT, FFT Wiritten by Paul Bourke, June 1993 void FFT(double *x,double *y, int n, long m) { long i,i1,j,k,i2,l,l1,l2; double c1,c2,tx,ty,t1,t2,u1,u2,z; short int dir = 0; /* Do the bit reversal */ i2 = n >> 1; j = 0; for (i=0;i>= 1; } j += k; } /* Compute the FFT */ c1 = -1.0; c2 = 0.0; l2 = 1; for (l=0;l 20) //{ //writeCount = 0; //Serial.println(""); //} } uint8_t docode() { if (strcmp(code,".-") == 0) printascii(65); if (strcmp(code,"-...") == 0) printascii(66); if (strcmp(code,"-.-.") == 0) printascii(67); if (strcmp(code,"-..") == 0) printascii(68); if (strcmp(code,".") == 0) printascii(69); if (strcmp(code,"..-.") == 0) printascii(70); if (strcmp(code,"--.") == 0) printascii(71); if (strcmp(code,"....") == 0) printascii(72); if (strcmp(code,"..") == 0) printascii(73); if (strcmp(code,".---") == 0) printascii(74); if (strcmp(code,"-.-") == 0) printascii(75); if (strcmp(code,".-..") == 0) printascii(76); if (strcmp(code,"--") == 0) printascii(77); if (strcmp(code,"-.") == 0) printascii(78); if (strcmp(code,"---") == 0) printascii(79); if (strcmp(code,".--.") == 0) printascii(80); if (strcmp(code,"--.-") == 0) printascii(81); if (strcmp(code,".-.") == 0) printascii(82); if (strcmp(code,"...") == 0) printascii(83); if (strcmp(code,"-") == 0) printascii(84); if (strcmp(code,"..-") == 0) printascii(85); if (strcmp(code,"...-") == 0) printascii(86); if (strcmp(code,".--") == 0) printascii(87); if (strcmp(code,"-..-") == 0) printascii(88); if (strcmp(code,"-.--") == 0) printascii(89); if (strcmp(code,"--..") == 0) printascii(90); if (strcmp(code,".----") == 0) printascii(49); if (strcmp(code,"..---") == 0) printascii(50); if (strcmp(code,"...--") == 0) printascii(51); if (strcmp(code,"....-") == 0) printascii(52); if (strcmp(code,".....") == 0) printascii(53); if (strcmp(code,"-....") == 0) printascii(54); if (strcmp(code,"--...") == 0) printascii(55); if (strcmp(code,"---..") == 0) printascii(56); if (strcmp(code,"----.") == 0) printascii(57); if (strcmp(code,"-----") == 0) printascii(48); if (strcmp(code,"..--..") == 0) printascii(63); if (strcmp(code,".-.-.-") == 0) printascii(46); if (strcmp(code,"--..--") == 0) printascii(44); if (strcmp(code,"-.-.--") == 0) printascii(33); if (strcmp(code,".--.-.") == 0) printascii(64); if (strcmp(code,"---...") == 0) printascii(58); if (strcmp(code,"-....-") == 0) printascii(45); if (strcmp(code,"-..-.") == 0) printascii(47); if (strcmp(code,"-.--.") == 0) printascii(40); if (strcmp(code,"-.--.-") == 0) printascii(41); if (strcmp(code,".-...") == 0) printascii(95); if (strcmp(code,"...-..-") == 0) printascii(36); if (strcmp(code,"...-.-") == 0) printascii(62); if (strcmp(code,".-.-.") == 0) printascii(60); if (strcmp(code,"...-.") == 0) printascii(126); ////////////////// // The specials // ////////////////// if (strcmp(code,".-.-") == 0) printascii(3); if (strcmp(code,"---.") == 0) printascii(4); if (strcmp(code,".--.-") == 0) printascii(6); } void Decode_Morse(float magnitude) { //magnitudelimit auto Increase if (magnitude > magnitudelimit_low) { magnitudelimit = (magnitudelimit +((magnitude - magnitudelimit)/6)); /// moving average filter } if (magnitudelimit < magnitudelimit_low) magnitudelimit = magnitudelimit_low; if(magnitude > magnitudelimit*0.6) // just to have some space up realstate = HIGH; else realstate = LOW; if (realstate != realstatebefore) laststarttime = millis(); if ((millis()-laststarttime) > nbtime) { if (realstate != filteredstate) { filteredstate = realstate; } } if (filteredstate != filteredstatebefore) { if (filteredstate == HIGH) { starttimehigh = millis(); lowduration = (millis() - startttimelow); } if (filteredstate == LOW) { startttimelow = millis(); highduration = (millis() - starttimehigh); if (highduration < (2*hightimesavg) || hightimesavg == 0) { hightimesavg = (highduration+hightimesavg+hightimesavg)/3; // now we know avg dit time ( rolling 3 avg) } if (highduration > (5*hightimesavg) ) { hightimesavg = highduration+hightimesavg; // if speed decrease fast .. } } } /////////////////////////////////////////////////////////////// // now we will check which kind of baud we have - dit or dah // // and what kind of pause we do have 1 - 3 or 7 pause // // we think that hightimeavg = 1 bit // /////////////////////////////////////////////////////////////// if (filteredstate != filteredstatebefore) { stop = LOW; if (filteredstate == LOW) { if (highduration < (hightimesavg*2) && highduration > (hightimesavg*0.6)) /// 0.6 filter out false dits { strcat(code,"."); } if (highduration > (hightimesavg*2) && highduration < (hightimesavg*6)) { strcat(code,"-"); wpm = (wpm + (1200/((highduration)/3)))/2; //// the most precise we can do ;o) } } if (filteredstate == HIGH) { float lacktime = 1; if(wpm > 25)lacktime=1.0; /// when high speeds we have to have a little more pause before new letter or new word if(wpm > 30)lacktime=1.2; if(wpm > 35)lacktime=1.5; if (lowduration > (hightimesavg*(2*lacktime)) && lowduration < hightimesavg*(5*lacktime)) // letter space { docode(); code[0] = '\0'; } if (lowduration >= hightimesavg*(5*lacktime)) { // word space docode(); code[0] = '\0'; printascii(32); } } } if ((millis() - startttimelow) > (highduration * 6) && stop == LOW) { docode(); code[0] = '\0'; stop = HIGH; } /* if(filteredstate == HIGH) { digitalWrite(ledPin, HIGH); tone(audioOutPin,target_freq); } else { digitalWrite(ledPin, LOW); noTone(audioOutPin); } */ realstatebefore = realstate; lasthighduration = highduration; filteredstatebefore = filteredstate; }