home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / dsp / dspgroup / asms.arc / 1KOLD.C next >
Encoding:
C/C++ Source or Header  |  1987-12-09  |  8.2 KB  |  370 lines

  1. #include    <stdio.h>
  2. #include    <dos.h>
  3. #include    <memory.h>
  4. #include    <process.h>
  5. #include        <conio.h>
  6. #define    CONTROL 0x34
  7. #define    CONT2 0xb4
  8. #define COMLINE 62        /*  (f4)  */
  9. #define THRESH 63     /*   (f5)  */
  10. #define UP 72
  11. #define DOWN 80
  12. #define PGUP 73
  13. #define PGDOWN 81
  14. #define RIGHT 77
  15. #define LEFT 75  
  16. #define CNRIGHT 116
  17. #define CNLEFT 115  
  18. #define HOME 71
  19. #define END 79
  20. #define CNHOME 119
  21. #define CNEND 117
  22. #define CLOSE 61         /* (f3)   */
  23. #define TCONSTU 60       /* (f2)  */ 
  24. #define TCONSTD 59       /* (f1)  */ 
  25. #define TCONST_NEW 68    /* (f10) */
  26. #define GO inp(0x306)
  27. #define STOP inp(0x307)
  28. unsigned int seg320,io320,prt0,cntrlpt,prt1,prt2,zl[512];
  29. cdecl sigpts();
  30. int spect_count,y[512],zh[512],threshold;
  31. long z[512];
  32. float norm[512],trate,freqmax;
  33. char *adr1, *adr2;
  34. cdecl main(argc,argv)
  35. int argc;
  36. char *argv[];
  37. {
  38. char c,scr_csts();
  39. int cmin,j,jh,k,i,scale,offset,kount;
  40. int ct,binmax;
  41. long count,jjj,jj,jjmax,jjmore,jjcnt;
  42. float zhold[513],zsum[512],tconst,rate,osc,amplitude;
  43.  
  44. seg320=0xd000;
  45. adr1 = (char *)0xD0001800L;
  46. adr2 = (char *)0xD0001C00L;
  47. tconst=0.5;
  48. jjcnt=0;
  49. io320=0x300;            /*  base i/o address for 320 board */
  50. osc = 25e6/4.;          /*  basic board oscillator */
  51. cntrlpt = io320 + 7;
  52. prt0 = io320 + 4;
  53. prt2 = io320 + 6;
  54. scale = 5;
  55. jjmore = 0;
  56. offset = 180;
  57. clark: do {
  58.     cls();
  59.     printf("Input desired sample rate in Hz ");
  60.     scanf( "%f",&rate);
  61. } while ( (rate < 80.0 ) || (rate > 40000.1));
  62. /* count gives you the oscillator "divisor" just as in SIO's */
  63. count = osc/rate;
  64. cmin = osc /40000.0;
  65. if ( count < 2 ) count = 2;
  66. trate = osc /(float)count;
  67. printf("\nRate = %f ",trate);
  68. /* set timers for desired sampling rate */
  69. control_timer(count,osc,&trate);
  70. scr_rowcol(24,1);
  71. printf("%7.2f Hz     ",trate);
  72. for(k=0;k<20000;k++) {       /* time delay for 8254 & clear holding arrays */
  73.       
  74.     if (k<513) {
  75.         zhold[k]=0.;
  76.     if (k<512) norm[k]=0.;
  77.     }
  78. }     
  79.  
  80. STOP;   /* halt */
  81. /* Load the fft and square routines */
  82. spawnl(P_WAIT,"intel",argv[0],"ffthy1k","d000","300","0",NULL);
  83. /* Run it once to set up arrays and have proper values in the ports */
  84. GO; /* go */
  85. while(inp(io320)!=88) {};
  86. STOP; /* stop */
  87. /* Generate Reference Baseline Spectrum */
  88.     printf("\n How many samples in reference spectrum? ");
  89.     scanf("%d",&spect_count);
  90. ref_spect(spect_count);
  91. STOP;
  92. printf("\nReference spectrum --- Hit any character when ready");
  93. ci(); co80(); scr_rowcol(1,1);
  94.     printf("\n How many samples for each displayed spectrum? ");
  95.     scanf("%d",&spect_count);
  96.  
  97. /* Set it up to run forever until ^C or F3 to change screen and quit*/
  98. hres(); kount=0; 
  99. GO;
  100. while (1) 
  101. {
  102.     if ((c = scr_csts()) != 0 )
  103.         {
  104.          switch(c) {
  105.              case COMLINE: {
  106.                  co80();
  107.                  goto clark;
  108.                 break;
  109.             }
  110.             case THRESH: {
  111.                 co80();
  112.                 printf("Input threshold %d? ",threshold);
  113.                 scanf("%d",&threshold);
  114.                 if (threshold < 1) threshold=0;
  115.                 if (threshold > 199) threshold = 199;
  116.                 if (threshold==0) outp(97,inp(97)&0xFC);
  117.                 hres();
  118.                 break;
  119.             }
  120.             case UP: {
  121.                 scale -= 1;
  122.                 if (scale < 0) scale = 0;
  123.                 break;
  124.             }
  125.             case DOWN: {
  126.                 scale += 1;
  127.                 if (scale > 15) scale = 15;
  128.                 break;
  129.             }
  130.             case PGUP: {
  131.                 offset -= 10;
  132.                 if (offset < 0) offset = 0;
  133.                 break;
  134.             }
  135.             case PGDOWN: {
  136.                 offset += 10;
  137.                 if (offset > 180) offset = 180;
  138.                 break;
  139.             }
  140.             case CLOSE: {
  141.                 STOP; /* halt */
  142.                 outp(97,inp(97)&0xFC);
  143.                 co80();
  144.                 exit();
  145.                 break;
  146.             }
  147.             case HOME: {
  148.                 jjmore -= 1;
  149.                 if (jjmore < 0) jjmore = 0;
  150.                 break;
  151.             }
  152.             case END: {
  153.                 jjmore += 1;
  154.                 if (jjmore > 10000) jjmore = 10000;
  155.                 break;
  156.             }
  157.             case CNHOME: {
  158.                 jjmore /= 2;
  159.                 if (jjmore < 0) jjmore = 0;
  160.                 break;
  161.             }
  162.             case CNEND: {
  163.                 jjmore *= 2;
  164.                 if (jjmore > 10000) jjmore = 10000;
  165.                 break;
  166.             }
  167.                  case TCONSTU: {
  168.                 tconst *= 2.0;
  169.                 if (tconst>1.0) tconst=1.0;
  170.                 break;
  171.             }   
  172.             case TCONST_NEW: {
  173.                 co80(); printf("%3d\n",c);
  174.                 printf(" tconst currently = %9.7f --- Enter new value: ",tconst);
  175.                 scanf("%f",&tconst); cls();
  176.                 if (tconst>  1.0) tconst= 1.0;
  177.                 if (tconst<.0001) tconst=.0001 ;
  178.                 hres();
  179.                 break;
  180.             }
  181.                  case TCONSTD: {
  182.                 tconst *= 0.5;
  183.                 if (tconst<.0001) tconst=.0001 ;     
  184.                 break;
  185.             }   
  186.             default:
  187.                 break;
  188.         }
  189.     }
  190.  
  191. if (z[100]+z[175]+z[200]+z[250]+z[300]+z[400] > (1<<8))  /* is there data? */
  192.   {          /* bypass the following loop if there isn't */
  193. for (k=0;k<512;k++) {
  194.          if (kount == 0) {
  195.             zsum[k]=z[k];
  196.         }
  197.         else zsum[k] += z[k];
  198. }    
  199. kount++;
  200. if (kount >= spect_count)   {   
  201.         kount=0;
  202.     for (k=0;k<512;k++) zsum[k] *= norm[k];        
  203.         amplitude=0.;
  204.         binmax=128;
  205.     for(k=1;k<511;k++)        {
  206.         zhold[k]=(1.0-tconst)*zhold[k] +
  207.           (zsum[k]*2.0+zsum[k-1]+zsum[k+1])*0.25*tconst;
  208.         y[k]= offset-(((int)zhold[k])>>scale);
  209.             zsum[k] = 0;
  210.             if (y[k] < 1) y[k]=1;                  /* don't go off the top */
  211.         if (k > 80 && zhold[k]>amplitude) {    /* avoid DC peak */
  212.             binmax=k;
  213.             amplitude=zhold[k];
  214.         }
  215.     }
  216.         scr_rowcol(24,1);
  217.         freqmax=((float)binmax)*trate/1024.0;
  218.     printf("Rate= %5.0f Hz Scale= %d tconst= % 5.4f sampl= %d freq=% 5.0f amp=% 4.0f  ",
  219.             trate,scale,tconst,spect_count,freqmax,amplitude);
  220.     sigpts(&y[0],512);      /* show old fft */
  221.     if (threshold!=0){
  222.         if (y[binmax]<threshold) {
  223.             outp(97,inp(97)&0xFC);
  224.             prt1=1193180.0/freqmax;
  225.             outp(67,182);
  226.             outp(66,prt1&0xFF);
  227.             outp(66,(prt1>>8)&0xFF);
  228.             outp(97,inp(97)|3);
  229.     }
  230.     else outp(97,inp(97)&0xFC);
  231.     }
  232.  
  233.    }
  234.   }     /* end of bypass loop -- display counter even if no data at input */
  235.       
  236.         scr_rowcol(1,1);  printf("% 5d   ",jjcnt++);
  237.         if (jjcnt>32675) jjcnt=0;
  238.  
  239.     while(inp(io320)!=88) {};
  240.     STOP;  /* halt */
  241.     memcpy((char *)zh,adr1,1024);
  242.     memcpy((char *)zl,adr2,1024);
  243.     GO; /* go */
  244.     for(k=0;k<512;k++) {
  245.         z[k]=zh[k];
  246.         z[k]=(z[k]<<16)+zl[k];
  247.     }
  248.   }
  249. }
  250.  
  251. fft()
  252. {    
  253.     int k;
  254.     while(inp(io320)!=88) {};
  255.     STOP;  /* halt */
  256.     memcpy((char *)zh,adr1,1024);
  257.     memcpy((char *)zl,adr2,1024);
  258.     GO; /* go */
  259.     for(k=0;k<512;k++) {
  260.         z[k]=zh[k];
  261.         z[k]=(z[k]<<16)+zl[k];
  262.     }
  263. }
  264.  
  265. co80()
  266. {
  267.     printf("\x1B[=3h");
  268. }
  269.  
  270. cls()
  271. {
  272.     printf("\x1B[2J");
  273. }
  274.  
  275. hres()
  276. {
  277.     printf("\x1b[=6h");
  278. }
  279.  
  280. scr_rowcol(x,y)
  281. unsigned x,y;
  282. {
  283.     union REGS inregs,outregs;
  284.     inregs.x.ax=0x200;
  285.     inregs.x.bx=15;
  286.     inregs.x.dx=(x<<8)+y;
  287.     int86(0x10,&inregs,&outregs);
  288. }
  289.  
  290. ci()
  291. {
  292.     int j;
  293.     while (kbhit()==0) {};
  294.       j=getch();
  295. }
  296.  
  297. char scr_csts()
  298. {
  299.     char c;
  300.     if (kbhit()==0) return(0);
  301.     if (getch()!=0) return(0);
  302.     return(getch());
  303. }
  304.  
  305. control_timer(cnt,osc,trate)
  306. long cnt;
  307. float *trate,osc;
  308. {
  309.     int ctl,cth;
  310.     /* tell board timer info coming */
  311.     outp(cntrlpt ,CONTROL);
  312.     /* count is timing divisor */
  313.     ctl = cnt & 0xff;    /* lo byte */
  314.     cth = ( cnt >> 8 ) & 0xff;/* hi byte */
  315.     /* control timer 0 */
  316.     outp(prt0 , ctl);
  317.     outp(prt0 , cth);
  318.     /* control timer 2 */
  319.     outp(cntrlpt , CONT2);
  320.     outp(prt2 , ctl);
  321.     outp(prt2 , cth);
  322.     /* compute new sampling rate  for output on screen*/
  323.     *trate = osc/(float)cnt;
  324. }
  325. ref_spect(spect_count)
  326. int spect_count;
  327. {
  328. float t1,t2,t3,t4,norm_max;
  329. int binmax,jj,k;
  330.  
  331.         GO; /* go */
  332. for(k=0;k<512;k++) norm[k] = 0.0;
  333. for (jj=0;jj<spect_count;jj++) {
  334.         scr_rowcol(4,1);printf("% 5d  ",jj);
  335.             fft();
  336.         for(k=0;k<512;k++) norm[k] += z[k];
  337. }
  338. binmax=256;
  339. norm_max=-999.;
  340. t1 = t2 = t3 = t4 = 0.0;
  341. for (k=0;k<506;k++) {    /* start with 7-point running average smoothing */
  342.     t4=t3;
  343.     t3=t2;
  344.     t2=t1;
  345.     t1=norm[k]+norm[k+1]+norm[k+2]+norm[k+3]+norm[k+4]+norm[k+5]+norm[k+6];
  346.     norm[k]=t4/7.0;          /* keep centered at proper index */
  347.     
  348.        if ( norm[k] > norm_max ) {
  349.            binmax = k;
  350.            norm_max = norm[k] ;
  351.        }    
  352.    }
  353. for (k=0;k<25;k++) {
  354.        norm[k]=0.;
  355.        norm[511-k]=0.;
  356.      }  
  357. for (k=0;k<512;k++) {
  358.     y[k]=180 - (norm[k]/norm_max)*100.;
  359.     if ((norm[k]>100) && (k>60)) 
  360.                  norm[k] = norm_max/(norm[k]*512.0);
  361.     else         norm[k] = 0.;
  362. }
  363.         hres(); sigpts(&y[0],510);
  364.            scr_rowcol(24,1);
  365. printf("Rate = %7.2f Hz                          freq=% 7.1f amp=% 8.1f  ",
  366.             trate,         ((float)binmax)*trate/512.0,norm_max );
  367.  
  368. }
  369.  
  370.