home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / dsp / dspgroup / i2kbd.arc / SPEC_KBD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-28  |  10.8 KB  |  501 lines

  1. /*
  2.        AMSAT/TAPR Digital Signal Processing Project
  3.        FFT Spectrum Analizer for EME test
  4.        code by Bob McGwier N4HY
  5.            Tom Clark W3IWI
  6.            Alberto E. Zagni I2KBD
  7.            Maurizio Marcovati I2JDQ
  8.            rel 1.1     27th June 88
  9.  
  10. */
  11. #include        <stdio.h>
  12. #include    <dos.h>
  13. #include    <memory.h>
  14. #include    <process.h>
  15. #include        <conio.h>
  16. #include        <graph.h>
  17. #include        <sys/types.h>
  18. #include        <sys/timeb.h>
  19. #include        <time.h>
  20. #define    CONTROL 0x34
  21. #define    CONT2 0xb4
  22. #define COMLINE 62        /*  (f4)  */
  23. #define SLEEP 63        /*   (f5)  */
  24. #define UP 72
  25. #define DOWN 80
  26. #define PGUP 73
  27. #define PGDOWN 81
  28. #define RIGHT 77
  29. #define LEFT 75  
  30. #define CNRIGHT 116
  31. #define CNLEFT 115  
  32. #define HOME 71
  33. #define END 79
  34. #define CNHOME 119
  35. #define CNEND 117
  36. #define CLOSE 61         /* (f3)   */
  37. #define FREQUP 60       /* (f2)  */
  38. #define FREQDWN 59       /* (f1)  */
  39. #define TCONST_NEW 68    /* (f10) */
  40. #define CLEARSTA 64          /* F6 */
  41. #define R1DOWN 65        /* F7 */
  42. #define ALFAUP 66        /* F8 */
  43. #define ALFADWN 67       /* F9 */
  44. #define GO inp(0x306)
  45. #define STOP inp(0x307)
  46. unsigned int seg320,io320,prt0,cntrlpt,prt1,prt2,zl[512],can,scri;
  47. int spect_count,y[512],oy[512],zh[512],sstat[128],threshold,sm[105],peak,maxpeak,maxfre,topp;
  48. int slide[512],r1,r2,m,g,kmax,i1s,i2s,i1d,i2d,i,bs,bd,k,statnum,dopshift;
  49. int buf[512][10],myfreq,stat_fre,old_fre,sleep,ptt,byte_on,byte_off,add;
  50. long z[512];
  51. short style=0xFFFF;
  52. float trate,freqmax,mst;
  53. float norm[512],out[512],mult,alfa,thres,mean,mean1,mean2;
  54. char *adr1, *adr2;
  55. cdecl main(argc,argv)
  56. int argc;
  57. char *argv[];
  58. {
  59. struct timeb timebuffer;
  60. char c,scr_csts(),*timeline;
  61. int cmin,j,jh,k,i,scale,offset,kount;
  62. int ct,binmax,obmax,keydown;
  63. long count,jjj,jj,jjmax,jjmore,jjcnt,now,old;
  64. float zhold[513],zsum[512],tconst,rate,osc,amplitude;
  65.  
  66. seg320=0xd000;
  67. adr1 = (char *)0xD0001800L;
  68. adr2 = (char *)0xD0001C00L;
  69. ptt=888;
  70. byte_on=255;
  71. byte_off=0;
  72. keydown=1;
  73. tconst=0.5;
  74. jjcnt=0;
  75. io320=0x300;            /*  base i/o address for 320 board */
  76. osc = 25e6/4.;          /*  basic board oscillator */
  77. cntrlpt = io320 + 7;
  78. prt0 = io320 + 4;
  79. prt2 = io320 + 6;
  80. scale = 1;
  81. jjmore = 0;
  82. offset = 160;
  83. clark: do {
  84.     cls();
  85.     printf("     AMSAT/TAPR Digital Signal Processing project\n");
  86.     printf("         FFT Spectrum Analizer for EME test\n");
  87.     printf("     code by Bob N4HY - Tom W3IWI - Alberto I2KBD\n");
  88.     printf(" \n");
  89.        /*  printf("Input desired sample rate in Hz ");
  90.     scanf( "%f",&rate); */
  91.     rate=5120;
  92. } while ( (rate < 80.0 ) || (rate > 40000.1));
  93. /* count gives you the oscillator "divisor" just as in SIO's */
  94. count = osc/rate;
  95. cmin = osc /40000.0;
  96. if ( count < 2 ) count = 2;
  97. trate = osc /(float)count;
  98. printf("\nRate = %f ",trate);
  99. /* set timers for desired sampling rate */
  100. control_timer(count,osc,&trate);
  101. printf("%7.2f Hz     ",trate);
  102. for(k=0;k<20000;k++) {       /* time delay for 8254 & clear holding arrays */
  103.       
  104.     if (k<513) {
  105.         zhold[k]=0.;
  106.     if (k<512) norm[k]=0.;
  107.     }
  108. }     
  109.  
  110. STOP;   /* halt */
  111. for(k=0;k<105;k++) sm[k]=0;
  112.  
  113. for(i=0;i<512;i++)
  114.  {
  115.  oy[i]=0;
  116.  for(k=0;k<=9;k++) buf[i][k]=-1;
  117.  }
  118.  
  119.  for(i=0;i<512;i++) slide[i]=0;
  120.  
  121.  m=16; g=2; r1=3; r2=4; alfa=1.5;
  122.  bs=r2; bd=0; kmax=511-m-g-1; mult=alfa/m;
  123.  can=0; scri=1;
  124. /* Load the fft and square routines */
  125. spawnl(P_WAIT,"intel",argv[0],"ffthy1k","d000","300","0",NULL);
  126. /* Run it once to set up arrays and have proper values in the ports */
  127. GO; /* go */
  128. while(inp(io320)!=88) {};
  129. STOP; /* stop */
  130. /* Generate Reference Baseline Spectrum */
  131.     printf("\n How many samples in reference spectrum? ");
  132.     scanf("%d",&spect_count);
  133. ref_spect(spect_count);
  134. STOP;
  135. printf("\nReference spectrum --- Hit any character when ready");
  136. ci();
  137. _setvideomode(_TEXTC80);
  138. _settextposition(1,1);
  139.       /*  printf("\n How many samples for each displayed spectrum? ");
  140.     scanf("%d",&spect_count);  */
  141.     spect_count=1;
  142.     printf("\n Expected Doppler shift in Hz ");
  143.     scanf("%d",&dopshift);
  144.     stat_fre=(1200+dopshift)/20;
  145.     old_fre=stat_fre;
  146. /* Set it up to run forever until ^C or F3 to change screen and quit*/
  147. _setvideomode(_HRESBW);
  148. _setviewport(0,8,639,176);
  149. _setlinestyle(style);
  150. for(k=0;k<512;k+=20) {
  151.     _setpixel(k,164);
  152.     }
  153.     _moveto(0,165);
  154.     _lineto(0,167);
  155.     _moveto(200,165);
  156.     _lineto(200,167);
  157.     _moveto(400,165);
  158.     _lineto(400,167);
  159.     _setpixel(100,165);
  160.     _setpixel(300,165);
  161.     myfreq=(1200+dopshift)/5;
  162.     _moveto(myfreq-4,162);
  163.     _lineto(myfreq+4,162);
  164.     _moveto(myfreq-4,163);
  165.     _lineto(myfreq+4,163);
  166. count=0;
  167. GO;
  168. while (1) 
  169. {
  170.     if ((c = scr_csts()) != 0 )
  171.         {
  172.          switch(c) {
  173.              case COMLINE: {
  174.                  _setvideomode(_TEXTC80);;
  175.                  goto clark;
  176.                 break;
  177.             }
  178.             case SLEEP: {
  179.                 if (sleep==1) sleep=0;
  180.                 else sleep=1;
  181.                 break;
  182.             }
  183.             case UP: {
  184.                 scale -= 1;
  185.                 if (scale < 0) scale = 0;
  186.                 break;
  187.             }
  188.             case DOWN: {
  189.                 scale += 1;
  190.                 if (scale > 15) scale = 15;
  191.                 break;
  192.             }
  193.             case PGUP: {
  194.                 break;
  195.             }
  196.             case PGDOWN: {
  197.                 break;
  198.             }
  199.             case CLOSE: {
  200.                 STOP; /* halt */
  201.                 outp(97,inp(97)&0xFC);
  202.                 _setvideomode(_TEXTC80);;
  203.                 exit(0);
  204.                 break;
  205.             }
  206.             case HOME: {
  207.                 jjmore -= 1;
  208.                 if (jjmore < 0) jjmore = 0;
  209.                 break;
  210.             }
  211.             case END: {
  212.                 jjmore += 1;
  213.                 if (jjmore > 10000) jjmore = 10000;
  214.                 break;
  215.             }
  216.             case CLEARSTA: {
  217.                 _setcolor(0);
  218.                 for(k=0;k<128;k++) {
  219.                 sstat[k]=0;
  220.                 _moveto(k+500,160);
  221.                 _lineto(k+500,1);
  222.                 }
  223.                 _setcolor(1);
  224.                 break;
  225.             }
  226.             case R1DOWN: {
  227.                 break;
  228.             }
  229.             case ALFAUP: {
  230.                 break;
  231.             }
  232.             case ALFADWN: {
  233.                 break;
  234.             }
  235.             case CNHOME: {
  236.                 break;
  237.             }
  238.             case CNEND: {
  239.                 break;
  240.             }
  241.             case FREQUP: {
  242.                 stat_fre+=1;
  243.                 if (stat_fre>128) stat_fre=128;
  244.                 break;
  245.             }   
  246.             case TCONST_NEW: {
  247.                 break;
  248.             }
  249.             case FREQDWN: {
  250.                 stat_fre-=1;
  251.                 if (stat_fre<1) stat_fre=1;
  252.                 break;
  253.             }   
  254.             default:
  255.                 break;
  256.         }
  257.     }
  258.   if (keydown==1)
  259.   {
  260.   ftime(&timebuffer);
  261.   old=(timebuffer.time);
  262.   outp(ptt,byte_on);
  263.   while (now <= old+2)
  264.     {
  265.     ftime(&timebuffer);
  266.     now=(timebuffer.time);
  267.     }
  268.   outp(ptt,byte_off);
  269.   keydown=0;
  270.   ftime(&timebuffer);
  271.   old=(timebuffer.time);
  272.   add=1;
  273.   }
  274.   ftime(&timebuffer);
  275.   now=(timebuffer.time);
  276.   if (now>old+2) add=0;
  277.   if (now>old+4) keydown=1;
  278.  
  279. if (z[100]+z[175]+z[200]+z[250]+z[300]+z[400] > (1<<6))  /* is there data? */
  280.   {          /* bypass the following loop if there isn't */
  281.  
  282.     i1s=i1d=0;
  283.     i2s=i2d=m+2*g+1;
  284.     mean1=mean2=0;
  285.  
  286.  
  287.     for (k=0;k<512;k++)
  288.  
  289.         zhold[k]=z[k]*norm[k];
  290.  
  291.  
  292.     for(i=0;i<m;i++)
  293.     {
  294.         mean1=mean1+zhold[i1s];
  295.         mean2=mean2+zhold[i2s];
  296.         i1s++;  i2s++;
  297.     }
  298.     i=m+g+1;
  299.     while(i<kmax)
  300.     {
  301.         if(mean1>mean2)
  302.                  mean=mean1;
  303.             else mean=mean2;
  304.         thres=mean*mult;
  305.         if(zhold[i]>thres)
  306.         {buf[i][bs]=1; slide[i]++;}
  307.             else buf[i][bs]=-1;
  308.         if(buf[i][bd]>0) slide[i]--;
  309.         if(slide[i]>r1)
  310.             {
  311.             y[i]=(((int)zhold[i])>>scale);
  312.             if(y[i] > 160 ) y[i]=160;
  313.             }
  314.         else y[i]=0;
  315.         mean1=mean1+zhold[i1s]-zhold[i1d];
  316.         mean2=mean2+zhold[i2s]-zhold[i2d];
  317.         i1s++; i1d++; i2s++; i2d++; i++;
  318.     }
  319.  
  320.   /* this is for sliding window detector */
  321.  
  322.  
  323.     bs++; if(bs>8) bs=0;
  324.     bd++; if(bd>8) bd=0;
  325.  
  326.     _setcolor(can);
  327.     for(k=20;k<490;k++){
  328.     if(oy[k]>0) {
  329.         _moveto(k,160);
  330.         _lineto(k,offset-oy[k]);
  331.         oy[k]=0;
  332.         }
  333.      }
  334.     _setcolor(scri);
  335.     maxpeak=0;
  336.     maxfre=0;
  337.     statnum+=1;
  338.     mst=100.0;
  339.     for(k=20;k<490;k++){
  340.  
  341.     if(y[k]>0) {if (y[k]>maxpeak) {
  342.                     maxpeak=y[k];
  343.                     maxfre=k;
  344.                     }
  345.         oy[k]=y[k];
  346.         if (add==1) sstat[(int)k/4]+=2;
  347.         if (add==0) sstat[(int)k/4]-=2;
  348.         _moveto(k,160);
  349.         _lineto(k,offset-y[k]);
  350.           }
  351.     }
  352.     if (statnum==10) {
  353.             _setcolor(0);
  354.     for(k=1;k<128;k++) {
  355.             _moveto(k+500,179);
  356.             _lineto(k+500,20);
  357.             }
  358.             _setcolor(1);
  359.     statnum=1;
  360.     for(k=10;k<110;k++){
  361.     _moveto(k+500,160);
  362.     if (sstat[k] >0) _lineto(k+500,160-sstat[k]);
  363.     }
  364.     _setcolor(0);
  365.     _moveto(old_fre+500,161);
  366.     _lineto(old_fre+500,164);
  367.     _setcolor(1);
  368.     _moveto(stat_fre+500,161);
  369.     _lineto(stat_fre+500,164);
  370.     old_fre=stat_fre;
  371.     _settextposition(24,66);
  372.     printf("% 5d Hz",stat_fre*20);
  373.     }
  374.    /* SIGPTS(&y[0],512);*/      /* show old fft */
  375.  
  376.   }     /* end of bypass loop -- display counter even if no data at input */
  377.       
  378.         _settextposition(1,1);  printf("% 5d   ",jjcnt++);
  379.         if (jjcnt>32675) jjcnt=0;
  380.         _settextposition(24,1);
  381.         printf("kbd ver 1.0                         peak: % 5d Hz",maxfre*5);
  382.  
  383.     while(inp(io320)!=88) {};
  384.     STOP;  /* halt */
  385.     memcpy((char *)zh,adr1,1024);
  386.     memcpy((char *)zl,adr2,1024);
  387.     GO; /* go */
  388.     for(k=0;k<512;k++) {
  389.         z[k]=zh[k];
  390.         z[k]=(z[k]<<16)+zl[k];
  391.     }
  392.   }
  393. }
  394.  
  395. cls()
  396. {
  397.     printf("\x1B[2J");
  398. }
  399.     
  400. ci()
  401. {
  402.     int j;
  403.     while (kbhit()==0) {};
  404.       j=getch();
  405. }
  406.  
  407. char scr_csts()
  408. {
  409.     char c;
  410.     if (kbhit()==0) return(0);
  411.     if (getch()!=0) return(0);
  412.     return(getch());
  413. }
  414.  
  415. control_timer(cnt,osc,trate)
  416. long cnt;
  417. float *trate,osc;
  418. {
  419.     int ctl,cth;
  420.     /* tell board timer info coming */
  421.     outp(cntrlpt ,CONTROL);
  422.     /* count is timing divisor */
  423.     ctl = cnt & 0xff;    /* lo byte */
  424.     cth = ( cnt >> 8 ) & 0xff;/* hi byte */
  425.     /* control timer 0 */
  426.     outp(prt0 , ctl);
  427.     outp(prt0 , cth);
  428.     /* control timer 2 */
  429.     outp(cntrlpt , CONT2);
  430.     outp(prt2 , ctl);
  431.     outp(prt2 , cth);
  432.     /* compute new sampling rate  for output on screen*/
  433.     *trate = osc/(float)cnt;
  434. }
  435. ref_spect(spect_count)
  436. int spect_count;
  437. {
  438. float t1,t2,t3,t4,norm_max;
  439. int binmax,jj,k;
  440.  
  441.         GO; /* go */
  442. for(k=0;k<512;k++) norm[k] = 0.0;
  443. for (jj=0;jj<spect_count;jj++) {
  444.         _settextposition(1,75);printf("% 5d  ",jj);
  445.     while(inp(io320)!=88) {};
  446.     STOP;  /* halt */
  447.     memcpy((char *)zh,adr1,1024);
  448.     memcpy((char *)zl,adr2,1024);
  449.     GO; /* go */
  450.     for(k=0;k<512;k++) {
  451.         z[k]=zh[k];
  452.         z[k]=(z[k]<<16)+zl[k];
  453.     }
  454.         for(k=0;k<512;k++) norm[k] += z[k];
  455. }
  456. binmax=256;
  457. norm_max=-999.;
  458. t1 = t2 = t3 = t4 = 0.0;
  459. for (k=0;k<506;k++) {    /* start with 7-point running average smoothing */
  460.     t4=t3;
  461.     t3=t2;
  462.     t2=t1;
  463.     t1=norm[k]+norm[k+1]+norm[k+2]+norm[k+3]+norm[k+4]+norm[k+5]+norm[k+6];
  464.     norm[k]=t4/7.0;          /* keep centered at proper index */
  465.     
  466.     if (k>80 && norm[k] > norm_max ) {
  467.            binmax = k;
  468.            norm_max = norm[k] ;
  469.        }    
  470.    }
  471. for (k=0;k<25;k++) {
  472.        norm[k]=0.;
  473.        norm[511-k]=0.;
  474.      }  
  475. for (k=0;k<512;k++) {
  476.     y[k]=180 - (norm[k]/norm_max)*100.;
  477.     if ((norm[k]>100) && (k>60)) 
  478.                  norm[k] = norm_max/(norm[k]*512.0);
  479.     else         norm[k] = 0.;
  480. }
  481.         _setvideomode(_HRESBW);
  482.         _setviewport(0,8,639,176);
  483.         SIGPTS(&y[0],510);
  484.          /* _setcolor(1);
  485.         for (k=0;k<512;k+=20) {
  486.              _setpixel(k,160);
  487.              _setpixel(k,161);
  488.              _setpixel(k,162);
  489.              }
  490.              _moveto(binmax,20);
  491.              _lineto(binmax,170); */
  492.            _settextposition(24,1);
  493. printf("Rate = %7.2f Hz                          freq=% 7.1f amp=% 8.1f  ",
  494.             trate,         ((float)binmax)*trate/1024.0,norm_max );
  495. }
  496.  
  497.  
  498.  
  499.  
  500.  
  501.