Playing with Phasing - part 2b - audio processing code

Target is Teensy 4.0 + SGTL5000 audio shield.  Mostly ZL2CTM's code again - he's done some good stuff (IMO)!  Filter coefficients were generated using Oak Hills filter design program.  Oak Hills site is gone, but the GUI filter design program (Windows only, unfortunately) can be found by searching the interweb.  Same disclaimer as part 2a.

_______________________________________________________________________________________________ 

#include <_Teensy.h>

#include <Wire.h>                          
#include <Audio.h>  // Teensy audio library

// Number of Filter Coefficients
#define NO_HILBERT_COEFFS 200  

#define SSB_SWITCH 3
#define FILTER_SWITCH 4

// Iowa Hills Hilbert transform filter coefficients
const short Hilbert_Plus_45_Coeffs[NO_HILBERT_COEFFS] = {
(short)(32768 *  0.00000187995362921356),
(short)(32768 *  0.00000101382681805262),
(short)(32768 * -0.00000041944920781134),
(short)(32768 * -0.00000206886261366890),
(short)(32768 * -0.00000328853616338165),
(short)(32768 * -0.00000322961068377481),
(short)(32768 * -0.00000104261721389776),
(short)(32768 *  0.00000383674523618538),
(short)(32768 *  0.00001138329298555618),
(short)(32768 *  0.00002075305565085728),
(short)(32768 *  0.00003022999953827821),
(short)(32768 *  0.00003742715125554241),
(short)(32768 *  0.00003976617153966799),
(short)(32768 *  0.00003518092489684932),
(short)(32768 *  0.00002290562406415120),
(short)(32768 *  0.00000414016449212795),
(short)(32768 * -0.00001763984073477332),
(short)(32768 * -0.00003692470636451617),
(short)(32768 * -0.00004705678293248639),
(short)(32768 * -0.00004169571548118256),
(short)(32768 * -0.00001661604801483918),
(short)(32768 *  0.00002856819992302917),
(short)(32768 *  0.00008924310532165691),
(short)(32768 *  0.00015566049842163920),
(short)(32768 *  0.00021412860951386392),
(short)(32768 *  0.00024954276159405708),
(short)(32768 *  0.00024891708744689822),
(short)(32768 *  0.00020523219427559525),
(short)(32768 *  0.00012067319039488211),
(short)(32768 *  0.00000828072279546177),
(short)(32768 * -0.00010876693704631180),
(short)(32768 * -0.00020058304653503001),
(short)(32768 * -0.00023643388703931123),
(short)(32768 * -0.00019207553123123944),
(short)(32768 * -0.00005703790884581394),
(short)(32768 *  0.00015996840375009924),
(short)(32768 *  0.00042924060835503042),
(short)(32768 *  0.00070302636595442891),
(short)(32768 *  0.00092342460993677378),
(short)(32768 *  0.00103426445275545120),
(short)(32768 *  0.00099482492078095675),
(short)(32768 *  0.00079241563798859715),
(short)(32768 *  0.00045058503746986389),
(short)(32768 *  0.00003020270378328860),
(short)(32768 * -0.00037809176137670875),
(short)(32768 * -0.00066977186361327767),
(short)(32768 * -0.00074829190270975232),
(short)(32768 * -0.00054877914953976870),
(short)(32768 * -0.00005827662607771344),
(short)(32768 *  0.00067276862682774663),
(short)(32768 *  0.00153185811359435320),
(short)(32768 *  0.00236034626141190529),
(short)(32768 *  0.00298246811144053936),
(short)(32768 *  0.00324279186315834522),
(short)(32768 *  0.00304473401047289371),
(short)(32768 *  0.00238144909963011742),
(short)(32768 *  0.00135089165996760130),
(short)(32768 *  0.00014931759505998343),
(short)(32768 * -0.00095839390996843576),
(short)(32768 * -0.00168814661446958780),
(short)(32768 * -0.00179734628181904554),
(short)(32768 * -0.00114480499178171158),
(short)(32768 *  0.00026374991284683347),
(short)(32768 *  0.00225671660155057907),
(short)(32768 *  0.00451200548559427261),
(short)(32768 *  0.00660791201516985893),
(short)(32768 *  0.00810285285115242004),
(short)(32768 *  0.00863009598106145859),
(short)(32768 *  0.00798853952437639236),
(short)(32768 *  0.00620909873396158218),
(short)(32768 *  0.00357905798591673374),
(short)(32768 *  0.00061377260135486722),
(short)(32768 * -0.00202476163394749165),
(short)(32768 * -0.00365150393918156624),
(short)(32768 * -0.00370277347974479198),
(short)(32768 * -0.00187310588080435991),
(short)(32768 *  0.00178553408477455378),
(short)(32768 *  0.00682784663513302803),
(short)(32768 *  0.01245793793350458145),
(short)(32768 *  0.01765142939984798431),
(short)(32768 *  0.02134133130311965942),
(short)(32768 *  0.02263728901743888855),
(short)(32768 *  0.02103775925934314728),
(short)(32768 *  0.01659170538187026978),
(short)(32768 *  0.00997202750295400620),
(short)(32768 *  0.00243640411645174026),
(short)(32768 * -0.00432921992614865303),
(short)(32768 * -0.00846713781356811523),
(short)(32768 * -0.00826646760106086731),
(short)(32768 * -0.00249097635969519615),
(short)(32768 *  0.00934264715760946274),
(short)(32768 *  0.02679026685655117035),
(short)(32768 *  0.04844862595200538635),
(short)(32768 *  0.07208253443241119385),
(short)(32768 *  0.09489956498146057129),
(short)(32768 *  0.11393431574106216431),
(short)(32768 *  0.12648330628871917725),
(short)(32768 *  0.13052044808864593506),
(short)(32768 *  0.12502384185791015625),
(short)(32768 *  0.11015775054693222046),
(short)(32768 *  0.08727697283029556274),
(short)(32768 *  0.05875028669834136963),
(short)(32768 *  0.02762975543737411499),
(short)(32768 * -0.00278118043206632137),
(short)(32768 * -0.02939149923622608185),
(short)(32768 * -0.04974408075213432312),
(short)(32768 * -0.06232902035117149353),
(short)(32768 * -0.06674224138259887695),
(short)(32768 * -0.06366980075836181641),
(short)(32768 * -0.05470800399780273438),
(short)(32768 * -0.04205619916319847107),
(short)(32768 * -0.02813878841698169708),
(short)(32768 * -0.01522193104028701782),
(short)(32768 * -0.00508764060214161873),
(short)(32768 *  0.00118558865506201982),
(short)(32768 *  0.00330774253234267235),
(short)(32768 *  0.00172494212165474892),
(short)(32768 * -0.00254931696690618992),
(short)(32768 * -0.00817991327494382858),
(short)(32768 * -0.01378324162214994431),
(short)(32768 * -0.01817758195102214813),
(short)(32768 * -0.02056903392076492310),
(short)(32768 * -0.02064667269587516785),
(short)(32768 * -0.01857941970229148865),
(short)(32768 * -0.01492566708475351334),
(short)(32768 * -0.01048160158097743988),
(short)(32768 * -0.00610282970592379570),
(short)(32768 * -0.00253540510311722755),
(short)(32768 * -0.00028686513542197645),
(short)(32768 *  0.00044281702139414847),
(short)(32768 * -0.00023613573284819722),
(short)(32768 * -0.00196024379692971706),
(short)(32768 * -0.00421178108081221581),
(short)(32768 * -0.00643614307045936584),
(short)(32768 * -0.00815287977457046509),
(short)(32768 * -0.00903960969299077988),
(short)(32768 * -0.00897559151053428650),
(short)(32768 * -0.00804105214774608612),
(short)(32768 * -0.00647746818140149117),
(short)(32768 * -0.00462108245119452477),
(short)(32768 * -0.00282579683698713779),
(short)(32768 * -0.00139185704756528139),
(short)(32768 * -0.00051368179265409708),
(short)(32768 * -0.00025485135847702622),
(short)(32768 * -0.00055192952277138829),
(short)(32768 * -0.00124285195488482714),
(short)(32768 * -0.00211122864857316017),
(short)(32768 * -0.00293577904812991619),
(short)(32768 * -0.00353441317565739155),
(short)(32768 * -0.00379485054872930050),
(short)(32768 * -0.00368738733232021332),
(short)(32768 * -0.00325958081521093845),
(short)(32768 * -0.00261628418229520321),
(short)(32768 * -0.00189098750706762075),
(short)(32768 * -0.00121538620442152023),
(short)(32768 * -0.00069354032166302204),
(short)(32768 * -0.00038519204827025533),
(short)(32768 * -0.00030032714130356908),
(short)(32768 * -0.00040448509389534593),
(short)(32768 * -0.00063220167066901922),
(short)(32768 * -0.00090467871632426977),
(short)(32768 * -0.00114747288171201944),
(short)(32768 * -0.00130460795480757952),
(short)(32768 * -0.00134678115136921406),
(short)(32768 * -0.00127289851661771536),
(short)(32768 * -0.00110566662624478340),
(short)(32768 * -0.00088308291742578149),
(short)(32768 * -0.00064822542481124401),
(short)(32768 * -0.00043971571722067893),
(short)(32768 * -0.00028470484539866447),
(short)(32768 * -0.00019540384528227150),
(short)(32768 * -0.00016927160322666168),
(short)(32768 * -0.00019218781380914152),
(short)(32768 * -0.00024342768301721662),
(short)(32768 * -0.00030108159990049899),
(short)(32768 * -0.00034670846071094275),
(short)(32768 * -0.00036839049425907433),
(short)(32768 * -0.00036184827331453562),
(short)(32768 * -0.00032975079375319183),
(short)(32768 * -0.00027971604140475392),
(short)(32768 * -0.00022168386203702539),
(short)(32768 * -0.00016534616588614881),
(short)(32768 * -0.00011817178165074438),
(short)(32768 * -0.00008432669710600749),
(short)(32768 * -0.00006453354581026360),
(short)(32768 * -0.00005669903839589097),
(short)(32768 * -0.00005700450492440723),
(short)(32768 * -0.00006111734546720982),
(short)(32768 * -0.00006522855983348563),
(short)(32768 * -0.00006672595918644220),
(short)(32768 * -0.00006443818710977212),
(short)(32768 * -0.00005849724766449071),
(short)(32768 * -0.00004994427581550553),
(short)(32768 * -0.00004023295696242712),
(short)(32768 * -0.00003077201108681038),
(short)(32768 * -0.00002260407563881017),
(short)(32768 * -0.00001626135053811595),
(short)(32768 * -0.00001178528145828750),
(short)(32768 * -0.00000886051384441089),
(short)(32768 * -0.00000699792735758820)
};

// Iowa Hills Hilbert transform filter coefficients
const short Hilbert_Minus_45_Coeffs[NO_HILBERT_COEFFS] = {
(short)(32768 * -0.00000699792735758820),
(short)(32768 * -0.00000886051384441089),
(short)(32768 * -0.00001178528145828750),
(short)(32768 * -0.00001626135053811595),
(short)(32768 * -0.00002260407563881017),
(short)(32768 * -0.00003077201108681038),
(short)(32768 * -0.00004023295696242712),
(short)(32768 * -0.00004994427581550553),
(short)(32768 * -0.00005849724766449071),
(short)(32768 * -0.00006443818710977212),
(short)(32768 * -0.00006672595918644220),
(short)(32768 * -0.00006522855983348563),
(short)(32768 * -0.00006111734546720982),
(short)(32768 * -0.00005700450492440723),
(short)(32768 * -0.00005669903839589097),
(short)(32768 * -0.00006453354581026360),
(short)(32768 * -0.00008432669710600749),
(short)(32768 * -0.00011817178165074438),
(short)(32768 * -0.00016534616588614881),
(short)(32768 * -0.00022168386203702539),
(short)(32768 * -0.00027971604140475392),
(short)(32768 * -0.00032975079375319183),
(short)(32768 * -0.00036184827331453562),
(short)(32768 * -0.00036839049425907433),
(short)(32768 * -0.00034670846071094275),
(short)(32768 * -0.00030108159990049899),
(short)(32768 * -0.00024342768301721662),
(short)(32768 * -0.00019218781380914152),
(short)(32768 * -0.00016927160322666168),
(short)(32768 * -0.00019540384528227150),
(short)(32768 * -0.00028470484539866447),
(short)(32768 * -0.00043971571722067893),
(short)(32768 * -0.00064822542481124401),
(short)(32768 * -0.00088308291742578149),
(short)(32768 * -0.00110566662624478340),
(short)(32768 * -0.00127289851661771536),
(short)(32768 * -0.00134678115136921406),
(short)(32768 * -0.00130460795480757952),
(short)(32768 * -0.00114747288171201944),
(short)(32768 * -0.00090467871632426977),
(short)(32768 * -0.00063220167066901922),
(short)(32768 * -0.00040448509389534593),
(short)(32768 * -0.00030032714130356908),
(short)(32768 * -0.00038519204827025533),
(short)(32768 * -0.00069354032166302204),
(short)(32768 * -0.00121538620442152023),
(short)(32768 * -0.00189098750706762075),
(short)(32768 * -0.00261628418229520321),
(short)(32768 * -0.00325958081521093845),
(short)(32768 * -0.00368738733232021332),
(short)(32768 * -0.00379485054872930050),
(short)(32768 * -0.00353441317565739155),
(short)(32768 * -0.00293577904812991619),
(short)(32768 * -0.00211122864857316017),
(short)(32768 * -0.00124285195488482714),
(short)(32768 * -0.00055192952277138829),
(short)(32768 * -0.00025485135847702622),
(short)(32768 * -0.00051368179265409708),
(short)(32768 * -0.00139185704756528139),
(short)(32768 * -0.00282579683698713779),
(short)(32768 * -0.00462108245119452477),
(short)(32768 * -0.00647746818140149117),
(short)(32768 * -0.00804105214774608612),
(short)(32768 * -0.00897559151053428650),
(short)(32768 * -0.00903960969299077988),
(short)(32768 * -0.00815287977457046509),
(short)(32768 * -0.00643614307045936584),
(short)(32768 * -0.00421178108081221581),
(short)(32768 * -0.00196024379692971706),
(short)(32768 * -0.00023613573284819722),
(short)(32768 *  0.00044281702139414847),
(short)(32768 * -0.00028686513542197645),
(short)(32768 * -0.00253540510311722755),
(short)(32768 * -0.00610282970592379570),
(short)(32768 * -0.01048160158097743988),
(short)(32768 * -0.01492566708475351334),
(short)(32768 * -0.01857941970229148865),
(short)(32768 * -0.02064667269587516785),
(short)(32768 * -0.02056903392076492310),
(short)(32768 * -0.01817758195102214813),
(short)(32768 * -0.01378324162214994431),
(short)(32768 * -0.00817991327494382858),
(short)(32768 * -0.00254931696690618992),
(short)(32768 *  0.00172494212165474892),
(short)(32768 *  0.00330774253234267235),
(short)(32768 *  0.00118558865506201982),
(short)(32768 * -0.00508764060214161873),
(short)(32768 * -0.01522193104028701782),
(short)(32768 * -0.02813878841698169708),
(short)(32768 * -0.04205619916319847107),
(short)(32768 * -0.05470800399780273438),
(short)(32768 * -0.06366980075836181641),
(short)(32768 * -0.06674224138259887695),
(short)(32768 * -0.06232902035117149353),
(short)(32768 * -0.04974408075213432312),
(short)(32768 * -0.02939149923622608185),
(short)(32768 * -0.00278118043206632137),
(short)(32768 *  0.02762975543737411499),
(short)(32768 *  0.05875028669834136963),
(short)(32768 *  0.08727697283029556274),
(short)(32768 *  0.11015775054693222046),
(short)(32768 *  0.12502384185791015625),
(short)(32768 *  0.13052044808864593506),
(short)(32768 *  0.12648330628871917725),
(short)(32768 *  0.11393431574106216431),
(short)(32768 *  0.09489956498146057129),
(short)(32768 *  0.07208253443241119385),
(short)(32768 *  0.04844862595200538635),
(short)(32768 *  0.02679026685655117035),
(short)(32768 *  0.00934264715760946274),
(short)(32768 * -0.00249097635969519615),
(short)(32768 * -0.00826646760106086731),
(short)(32768 * -0.00846713781356811523),
(short)(32768 * -0.00432921992614865303),
(short)(32768 *  0.00243640411645174026),
(short)(32768 *  0.00997202750295400620),
(short)(32768 *  0.01659170538187026978),
(short)(32768 *  0.02103775925934314728),
(short)(32768 *  0.02263728901743888855),
(short)(32768 *  0.02134133130311965942),
(short)(32768 *  0.01765142939984798431),
(short)(32768 *  0.01245793793350458145),
(short)(32768 *  0.00682784663513302803),
(short)(32768 *  0.00178553408477455378),
(short)(32768 * -0.00187310588080435991),
(short)(32768 * -0.00370277347974479198),
(short)(32768 * -0.00365150393918156624),
(short)(32768 * -0.00202476163394749165),
(short)(32768 *  0.00061377260135486722),
(short)(32768 *  0.00357905798591673374),
(short)(32768 *  0.00620909873396158218),
(short)(32768 *  0.00798853952437639236),
(short)(32768 *  0.00863009598106145859),
(short)(32768 *  0.00810285285115242004),
(short)(32768 *  0.00660791201516985893),
(short)(32768 *  0.00451200548559427261),
(short)(32768 *  0.00225671660155057907),
(short)(32768 *  0.00026374991284683347),
(short)(32768 * -0.00114480499178171158),
(short)(32768 * -0.00179734628181904554),
(short)(32768 * -0.00168814661446958780),
(short)(32768 * -0.00095839390996843576),
(short)(32768 *  0.00014931759505998343),
(short)(32768 *  0.00135089165996760130),
(short)(32768 *  0.00238144909963011742),
(short)(32768 *  0.00304473401047289371),
(short)(32768 *  0.00324279186315834522),
(short)(32768 *  0.00298246811144053936),
(short)(32768 *  0.00236034626141190529),
(short)(32768 *  0.00153185811359435320),
(short)(32768 *  0.00067276862682774663),
(short)(32768 * -0.00005827662607771344),
(short)(32768 * -0.00054877914953976870),
(short)(32768 * -0.00074829190270975232),
(short)(32768 * -0.00066977186361327767),
(short)(32768 * -0.00037809176137670875),
(short)(32768 *  0.00003020270378328860),
(short)(32768 *  0.00045058503746986389),
(short)(32768 *  0.00079241563798859715),
(short)(32768 *  0.00099482492078095675),
(short)(32768 *  0.00103426445275545120),
(short)(32768 *  0.00092342460993677378),
(short)(32768 *  0.00070302636595442891),
(short)(32768 *  0.00042924060835503042),
(short)(32768 *  0.00015996840375009924),
(short)(32768 * -0.00005703790884581394),
(short)(32768 * -0.00019207553123123944),
(short)(32768 * -0.00023643388703931123),
(short)(32768 * -0.00020058304653503001),
(short)(32768 * -0.00010876693704631180),
(short)(32768 *  0.00000828072279546177),
(short)(32768 *  0.00012067319039488211),
(short)(32768 *  0.00020523219427559525),
(short)(32768 *  0.00024891708744689822),
(short)(32768 *  0.00024954276159405708),
(short)(32768 *  0.00021412860951386392),
(short)(32768 *  0.00015566049842163920),
(short)(32768 *  0.00008924310532165691),
(short)(32768 *  0.00002856819992302917),
(short)(32768 * -0.00001661604801483918),
(short)(32768 * -0.00004169571548118256),
(short)(32768 * -0.00004705678293248639),
(short)(32768 * -0.00003692470636451617),
(short)(32768 * -0.00001763984073477332),
(short)(32768 *  0.00000414016449212795),
(short)(32768 *  0.00002290562406415120),
(short)(32768 *  0.00003518092489684932),
(short)(32768 *  0.00003976617153966799),
(short)(32768 *  0.00003742715125554241),
(short)(32768 *  0.00003022999953827821),
(short)(32768 *  0.00002075305565085728),
(short)(32768 *  0.00001138329298555618),
(short)(32768 *  0.00000383674523618538),
(short)(32768 * -0.00000104261721389776),
(short)(32768 * -0.00000322961068377481),
(short)(32768 * -0.00000328853616338165),
(short)(32768 * -0.00000206886261366890),
(short)(32768 * -0.00000041944920781134),
(short)(32768 *  0.00000101382681805262),
(short)(32768 *  0.00000187995362921356)
};


// Instantiate the Objects

AudioControlSGTL5000    audioShield;      // Name for the Teensy audio CODEC

// Audio shield
AudioInputI2S           audioInput;       // Name for the input to the audio shield
AudioOutputI2S          audioOutput;      // Name for the output of the audio shield
// Receiver
AudioFilterFIR          RX_Hilbert_Plus_45;    // Name for the RX +45 Hilbert transform
AudioFilterFIR          RX_Hilbert_Minus_45;   // Name for the RX +45 Hilbert transform
AudioFilterBiquad       biquad1;
AudioMixer4             RX_Summer;             / Name for the RX summer

// Audio connections
AudioConnection         patchCord5(audioInput, 0, RX_Hilbert_Plus_45, 0);    
AudioConnection         patchCord10(audioInput, 1, RX_Hilbert_Minus_45, 0);  
AudioConnection         patchCord15(RX_Hilbert_Plus_45, 0, RX_Summer, 0);     
AudioConnection         patchCord20(RX_Hilbert_Minus_45, 0, RX_Summer, 1);      
AudioConnection         patchCord30(RX_Summer, 0, biquad1, 0);
AudioConnection         patchCord40(biquad1, 0, audioOutput, 0);           
AudioConnection         patchCord45(biquad1, 0, audioOutput, 1);
bool sideband;
bool filter_mode;


void setup()
{

  // Setup the audio shield
  AudioNoInterrupts();
  AudioMemory(16);
  audioShield.enable();
  audioShield.inputSelect(AUDIO_INPUT_LINEIN);
  audioShield.lineInLevel(7, 7);
  audioShield.lineOutLevel(20, 20);
  audioShield.volume(1.0);
  audioShield.audioPreProcessorEnable();            
  audioShield.autoVolumeControl(0,1,0,-15,0.5,2.5);
  audioShield.autoVolumeEnable();
  AudioInterrupts();


  Turn_On_Receiver();
  pinMode(SSB_SWITCH, INPUT_PULLUP);
  pinMode(FILTER_SWITCH, INPUT_PULLUP);

}

void loop()
{
   sideband = digitalRead(SSB_SWITCH);
   filter_mode = digitalRead(FILTER_SWITCH);
   //Serial.println(sideband);
    if (sideband == 0)  //LSB
  {
    RX_Summer.gain(0, 3);
    RX_Summer.gain(1, -3.0);
  }

 else
  {
    RX_Summer.gain(0, 3);
    RX_Summer.gain(1, 3.0);
  }

  if (filter_mode == 0)
 {  biquad1.setBandpass(0, 800, 3.0);
    biquad1.setBandpass(1, 800, 3.0);
    biquad1.setBandpass(2, 800, 3.0);
    biquad1.setBandpass(3, 800, 3.0);
 }  

  else
  {
   biquad1.setBandpass(0, 1000, 0.4);
   biquad1.setBandpass(1, 1000, 0.4);
   biquad1.setBandpass(2, 1000, 0.4);
   biquad1.setBandpass(3, 1000, 0.4);
  }

}

void Turn_On_Receiver()
{
  AudioNoInterrupts();
  audioShield.muteHeadphone();                                           
  audioShield.unmuteLineout();
  RX_Hilbert_Plus_45.begin(Hilbert_Plus_45_Coeffs, NO_HILBERT_COEFFS);
  RX_Hilbert_Minus_45.begin(Hilbert_Minus_45_Coeffs, NO_HILBERT_COEFFS);
  AudioInterrupts();
}

Comments

Popular posts from this blog

Even a Blind Pig finds an acorn once and a while (35 watt push-pull RD30HVF1 amp for HF)

Playing with Phasing - part 1