home *** CD-ROM | disk | FTP | other *** search
- /*
- AMSAT/TAPR Digital Signal Processing Project
- FFT Spectrum Analizer for EME test
- code by Bob McGwier N4HY
- Tom Clark W3IWI
- Alberto E. Zagni I2KBD
- Maurizio Marcovati I2JDQ
- rel 1.1 27th June 88
-
- */
- #include <stdio.h>
- #include <dos.h>
- #include <memory.h>
- #include <process.h>
- #include <conio.h>
- #include <graph.h>
- #include <sys/types.h>
- #include <sys/timeb.h>
- #include <time.h>
- #define CONTROL 0x34
- #define CONT2 0xb4
- #define COMLINE 62 /* (f4) */
- #define SLEEP 63 /* (f5) */
- #define UP 72
- #define DOWN 80
- #define PGUP 73
- #define PGDOWN 81
- #define RIGHT 77
- #define LEFT 75
- #define CNRIGHT 116
- #define CNLEFT 115
- #define HOME 71
- #define END 79
- #define CNHOME 119
- #define CNEND 117
- #define CLOSE 61 /* (f3) */
- #define FREQUP 60 /* (f2) */
- #define FREQDWN 59 /* (f1) */
- #define TCONST_NEW 68 /* (f10) */
- #define CLEARSTA 64 /* F6 */
- #define R1DOWN 65 /* F7 */
- #define ALFAUP 66 /* F8 */
- #define ALFADWN 67 /* F9 */
- #define GO inp(0x306)
- #define STOP inp(0x307)
- unsigned int seg320,io320,prt0,cntrlpt,prt1,prt2,zl[512],can,scri;
- int spect_count,y[512],oy[512],zh[512],sstat[128],threshold,sm[105],peak,maxpeak,maxfre,topp;
- int slide[512],r1,r2,m,g,kmax,i1s,i2s,i1d,i2d,i,bs,bd,k,statnum,dopshift;
- int buf[512][10],myfreq,stat_fre,old_fre,sleep,ptt,byte_on,byte_off,add;
- long z[512];
- short style=0xFFFF;
- float trate,freqmax,mst;
- float norm[512],out[512],mult,alfa,thres,mean,mean1,mean2;
- char *adr1, *adr2;
- cdecl main(argc,argv)
- int argc;
- char *argv[];
- {
- struct timeb timebuffer;
- char c,scr_csts(),*timeline;
- int cmin,j,jh,k,i,scale,offset,kount;
- int ct,binmax,obmax,keydown;
- long count,jjj,jj,jjmax,jjmore,jjcnt,now,old;
- float zhold[513],zsum[512],tconst,rate,osc,amplitude;
-
- seg320=0xd000;
- adr1 = (char *)0xD0001800L;
- adr2 = (char *)0xD0001C00L;
- ptt=888;
- byte_on=255;
- byte_off=0;
- keydown=1;
- tconst=0.5;
- jjcnt=0;
- io320=0x300; /* base i/o address for 320 board */
- osc = 25e6/4.; /* basic board oscillator */
- cntrlpt = io320 + 7;
- prt0 = io320 + 4;
- prt2 = io320 + 6;
- scale = 1;
- jjmore = 0;
- offset = 160;
- clark: do {
- cls();
- printf(" AMSAT/TAPR Digital Signal Processing project\n");
- printf(" FFT Spectrum Analizer for EME test\n");
- printf(" code by Bob N4HY - Tom W3IWI - Alberto I2KBD\n");
- printf(" \n");
- /* printf("Input desired sample rate in Hz ");
- scanf( "%f",&rate); */
- rate=5120;
- } while ( (rate < 80.0 ) || (rate > 40000.1));
- /* count gives you the oscillator "divisor" just as in SIO's */
- count = osc/rate;
- cmin = osc /40000.0;
- if ( count < 2 ) count = 2;
- trate = osc /(float)count;
- printf("\nRate = %f ",trate);
- /* set timers for desired sampling rate */
- control_timer(count,osc,&trate);
- printf("%7.2f Hz ",trate);
- for(k=0;k<20000;k++) { /* time delay for 8254 & clear holding arrays */
-
- if (k<513) {
- zhold[k]=0.;
- if (k<512) norm[k]=0.;
- }
- }
-
- STOP; /* halt */
- for(k=0;k<105;k++) sm[k]=0;
-
- for(i=0;i<512;i++)
- {
- oy[i]=0;
- for(k=0;k<=9;k++) buf[i][k]=-1;
- }
-
- for(i=0;i<512;i++) slide[i]=0;
-
- m=16; g=2; r1=3; r2=4; alfa=1.5;
- bs=r2; bd=0; kmax=511-m-g-1; mult=alfa/m;
- can=0; scri=1;
- /* Load the fft and square routines */
- spawnl(P_WAIT,"intel",argv[0],"ffthy1k","d000","300","0",NULL);
- /* Run it once to set up arrays and have proper values in the ports */
- GO; /* go */
- while(inp(io320)!=88) {};
- STOP; /* stop */
- /* Generate Reference Baseline Spectrum */
- printf("\n How many samples in reference spectrum? ");
- scanf("%d",&spect_count);
- ref_spect(spect_count);
- STOP;
- printf("\nReference spectrum --- Hit any character when ready");
- ci();
- _setvideomode(_TEXTC80);
- _settextposition(1,1);
- /* printf("\n How many samples for each displayed spectrum? ");
- scanf("%d",&spect_count); */
- spect_count=1;
- printf("\n Expected Doppler shift in Hz ");
- scanf("%d",&dopshift);
- stat_fre=(1200+dopshift)/20;
- old_fre=stat_fre;
- /* Set it up to run forever until ^C or F3 to change screen and quit*/
- _setvideomode(_HRESBW);
- _setviewport(0,8,639,176);
- _setlinestyle(style);
- for(k=0;k<512;k+=20) {
- _setpixel(k,164);
- }
- _moveto(0,165);
- _lineto(0,167);
- _moveto(200,165);
- _lineto(200,167);
- _moveto(400,165);
- _lineto(400,167);
- _setpixel(100,165);
- _setpixel(300,165);
- myfreq=(1200+dopshift)/5;
- _moveto(myfreq-4,162);
- _lineto(myfreq+4,162);
- _moveto(myfreq-4,163);
- _lineto(myfreq+4,163);
- count=0;
- GO;
- while (1)
- {
- if ((c = scr_csts()) != 0 )
- {
- switch(c) {
- case COMLINE: {
- _setvideomode(_TEXTC80);;
- goto clark;
- break;
- }
- case SLEEP: {
- if (sleep==1) sleep=0;
- else sleep=1;
- break;
- }
- case UP: {
- scale -= 1;
- if (scale < 0) scale = 0;
- break;
- }
- case DOWN: {
- scale += 1;
- if (scale > 15) scale = 15;
- break;
- }
- case PGUP: {
- break;
- }
- case PGDOWN: {
- break;
- }
- case CLOSE: {
- STOP; /* halt */
- outp(97,inp(97)&0xFC);
- _setvideomode(_TEXTC80);;
- exit(0);
- break;
- }
- case HOME: {
- jjmore -= 1;
- if (jjmore < 0) jjmore = 0;
- break;
- }
- case END: {
- jjmore += 1;
- if (jjmore > 10000) jjmore = 10000;
- break;
- }
- case CLEARSTA: {
- _setcolor(0);
- for(k=0;k<128;k++) {
- sstat[k]=0;
- _moveto(k+500,160);
- _lineto(k+500,1);
- }
- _setcolor(1);
- break;
- }
- case R1DOWN: {
- break;
- }
- case ALFAUP: {
- break;
- }
- case ALFADWN: {
- break;
- }
- case CNHOME: {
- break;
- }
- case CNEND: {
- break;
- }
- case FREQUP: {
- stat_fre+=1;
- if (stat_fre>128) stat_fre=128;
- break;
- }
- case TCONST_NEW: {
- break;
- }
- case FREQDWN: {
- stat_fre-=1;
- if (stat_fre<1) stat_fre=1;
- break;
- }
- default:
- break;
- }
- }
- if (keydown==1)
- {
- ftime(&timebuffer);
- old=(timebuffer.time);
- outp(ptt,byte_on);
- while (now <= old+2)
- {
- ftime(&timebuffer);
- now=(timebuffer.time);
- }
- outp(ptt,byte_off);
- keydown=0;
- ftime(&timebuffer);
- old=(timebuffer.time);
- add=1;
- }
- ftime(&timebuffer);
- now=(timebuffer.time);
- if (now>old+2) add=0;
- if (now>old+4) keydown=1;
-
- if (z[100]+z[175]+z[200]+z[250]+z[300]+z[400] > (1<<6)) /* is there data? */
- { /* bypass the following loop if there isn't */
-
- i1s=i1d=0;
- i2s=i2d=m+2*g+1;
- mean1=mean2=0;
-
-
- for (k=0;k<512;k++)
-
- zhold[k]=z[k]*norm[k];
-
-
- for(i=0;i<m;i++)
- {
- mean1=mean1+zhold[i1s];
- mean2=mean2+zhold[i2s];
- i1s++; i2s++;
- }
- i=m+g+1;
- while(i<kmax)
- {
- if(mean1>mean2)
- mean=mean1;
- else mean=mean2;
- thres=mean*mult;
- if(zhold[i]>thres)
- {buf[i][bs]=1; slide[i]++;}
- else buf[i][bs]=-1;
- if(buf[i][bd]>0) slide[i]--;
- if(slide[i]>r1)
- {
- y[i]=(((int)zhold[i])>>scale);
- if(y[i] > 160 ) y[i]=160;
- }
- else y[i]=0;
- mean1=mean1+zhold[i1s]-zhold[i1d];
- mean2=mean2+zhold[i2s]-zhold[i2d];
- i1s++; i1d++; i2s++; i2d++; i++;
- }
-
- /* this is for sliding window detector */
-
-
- bs++; if(bs>8) bs=0;
- bd++; if(bd>8) bd=0;
-
- _setcolor(can);
- for(k=20;k<490;k++){
- if(oy[k]>0) {
- _moveto(k,160);
- _lineto(k,offset-oy[k]);
- oy[k]=0;
- }
- }
- _setcolor(scri);
- maxpeak=0;
- maxfre=0;
- statnum+=1;
- mst=100.0;
- for(k=20;k<490;k++){
-
- if(y[k]>0) {if (y[k]>maxpeak) {
- maxpeak=y[k];
- maxfre=k;
- }
- oy[k]=y[k];
- if (add==1) sstat[(int)k/4]+=2;
- if (add==0) sstat[(int)k/4]-=2;
- _moveto(k,160);
- _lineto(k,offset-y[k]);
- }
- }
- if (statnum==10) {
- _setcolor(0);
- for(k=1;k<128;k++) {
- _moveto(k+500,179);
- _lineto(k+500,20);
- }
- _setcolor(1);
- statnum=1;
- for(k=10;k<110;k++){
- _moveto(k+500,160);
- if (sstat[k] >0) _lineto(k+500,160-sstat[k]);
- }
- _setcolor(0);
- _moveto(old_fre+500,161);
- _lineto(old_fre+500,164);
- _setcolor(1);
- _moveto(stat_fre+500,161);
- _lineto(stat_fre+500,164);
- old_fre=stat_fre;
- _settextposition(24,66);
- printf("% 5d Hz",stat_fre*20);
- }
- /* SIGPTS(&y[0],512);*/ /* show old fft */
-
- } /* end of bypass loop -- display counter even if no data at input */
-
- _settextposition(1,1); printf("% 5d ",jjcnt++);
- if (jjcnt>32675) jjcnt=0;
- _settextposition(24,1);
- printf("kbd ver 1.0 peak: % 5d Hz",maxfre*5);
-
- while(inp(io320)!=88) {};
- STOP; /* halt */
- memcpy((char *)zh,adr1,1024);
- memcpy((char *)zl,adr2,1024);
- GO; /* go */
- for(k=0;k<512;k++) {
- z[k]=zh[k];
- z[k]=(z[k]<<16)+zl[k];
- }
- }
- }
-
- cls()
- {
- printf("\x1B[2J");
- }
-
- ci()
- {
- int j;
- while (kbhit()==0) {};
- j=getch();
- }
-
- char scr_csts()
- {
- char c;
- if (kbhit()==0) return(0);
- if (getch()!=0) return(0);
- return(getch());
- }
-
- control_timer(cnt,osc,trate)
- long cnt;
- float *trate,osc;
- {
- int ctl,cth;
- /* tell board timer info coming */
- outp(cntrlpt ,CONTROL);
- /* count is timing divisor */
- ctl = cnt & 0xff; /* lo byte */
- cth = ( cnt >> 8 ) & 0xff;/* hi byte */
- /* control timer 0 */
- outp(prt0 , ctl);
- outp(prt0 , cth);
- /* control timer 2 */
- outp(cntrlpt , CONT2);
- outp(prt2 , ctl);
- outp(prt2 , cth);
- /* compute new sampling rate for output on screen*/
- *trate = osc/(float)cnt;
- }
- ref_spect(spect_count)
- int spect_count;
- {
- float t1,t2,t3,t4,norm_max;
- int binmax,jj,k;
-
- GO; /* go */
- for(k=0;k<512;k++) norm[k] = 0.0;
- for (jj=0;jj<spect_count;jj++) {
- _settextposition(1,75);printf("% 5d ",jj);
- while(inp(io320)!=88) {};
- STOP; /* halt */
- memcpy((char *)zh,adr1,1024);
- memcpy((char *)zl,adr2,1024);
- GO; /* go */
- for(k=0;k<512;k++) {
- z[k]=zh[k];
- z[k]=(z[k]<<16)+zl[k];
- }
- for(k=0;k<512;k++) norm[k] += z[k];
- }
- binmax=256;
- norm_max=-999.;
- t1 = t2 = t3 = t4 = 0.0;
- for (k=0;k<506;k++) { /* start with 7-point running average smoothing */
- t4=t3;
- t3=t2;
- t2=t1;
- t1=norm[k]+norm[k+1]+norm[k+2]+norm[k+3]+norm[k+4]+norm[k+5]+norm[k+6];
- norm[k]=t4/7.0; /* keep centered at proper index */
-
- if (k>80 && norm[k] > norm_max ) {
- binmax = k;
- norm_max = norm[k] ;
- }
- }
- for (k=0;k<25;k++) {
- norm[k]=0.;
- norm[511-k]=0.;
- }
- for (k=0;k<512;k++) {
- y[k]=180 - (norm[k]/norm_max)*100.;
- if ((norm[k]>100) && (k>60))
- norm[k] = norm_max/(norm[k]*512.0);
- else norm[k] = 0.;
- }
- _setvideomode(_HRESBW);
- _setviewport(0,8,639,176);
- SIGPTS(&y[0],510);
- /* _setcolor(1);
- for (k=0;k<512;k+=20) {
- _setpixel(k,160);
- _setpixel(k,161);
- _setpixel(k,162);
- }
- _moveto(binmax,20);
- _lineto(binmax,170); */
- _settextposition(24,1);
- printf("Rate = %7.2f Hz freq=% 7.1f amp=% 8.1f ",
- trate, ((float)binmax)*trate/1024.0,norm_max );
- }
-
-
-
-
-
-