⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graph1.c

📁 三星公司的有线数字高频头资料及源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
							case 2 : qam_samples_to_store = 2560 ; break;
							case 3 : qam_samples_to_store = 10000; break;
							default: qam_samples_to_store = 0;
				 		}
				 		if (qam_samples_to_store > 0) {
							store_qam_to_file = 1;
							fp = fopen("data\\qam.dat","w");
							fprintf(fp,"# QAM Constellation Data\n");
							fprintf(fp,"# Format is decimal\n");
				 		}
			  	}
			  	break;

		  	case 9 : /* run script file */
				My_Acquisition();
//papi				qam_init(qam_status.acq_script);
			  	qam_read_status();
			  	nsamples=0;
			 	 	break;

		  	case 10: 
        	done=1;
			  	break;

			}
			fkeys[0] = (reduced_display) ? "Zoom In"        : "Zoom Out";
			fkeys[3] = (accumulate)      ? "Accumulate Off" : "Accumulate On";
			fkey_menu(1,fkey_win, fkeys);
    }
  } /* end while */

  /* restore text mode */
  closegraph();
  return QAM_OK;
}



/* compiler constants */
#define EQU_BORDER      10
#define EQU_HALF_BORDER 10
#define MAX_TAP_VALUE   32767
#define EQU_NHASH       6
#define FORWARD_FFT     1
#define DURATION        128

/* SCALING OF DFE/FFE:  FFE values are +/-8, DFE values are +/-1 range
   FFE_SCALE below uses 2/2^15 so that an FFE main tap value of 4
   results in a 0 dB readout (so display is relative to gain of 4) */
#define FFE_SCALE       (2.0/32768.0)
#define DFE_SCALE       (1.0/32768.0)
#define FFT_SCALE       (1.0)
void equ_freq_response(int *ffe_tap, int *dfe_tap, float *freq,int num_ffe_taps,int num_dfe_taps)
{
  int   i,j,k;
  float tmpNum, tmpDen;
  float ffe[32],dfe[32];
  float i_ffe,q_ffe;
  float i_dfe,q_dfe;
  float i_hard,q_hard;
  float i_sum,q_sum;
  float *tmp_freq_ffe;
  float *tmp_freq_dfe;

  tmp_freq_ffe = malloc(2 * DURATION * sizeof(float));
  tmp_freq_dfe = malloc(2 * DURATION * sizeof(float));

  // zero the buffers 
  for (i=0; i<2*DURATION; i++) 
  	tmp_freq_dfe[i] = tmp_freq_ffe[i] = 0.0;

  // compute coefficients to float values 
  for (i=0; i<num_ffe_taps; i++) {
    ffe[2*i  ] = FFE_SCALE * ffe_tap[2*i];
    ffe[2*i+1] = FFE_SCALE * ffe_tap[2*i+1];
  }
  for (i=0; i<num_dfe_taps; i++) {
    dfe[2*i  ] = DFE_SCALE * dfe_tap[2*i];
    dfe[2*i+1] = DFE_SCALE * dfe_tap[2*i+1];
  }
  
  // do ffe, dfe buffer 
  tmp_freq_dfe[0] = 1.0;
  tmp_freq_dfe[1] = 0.0;

  for (i=0; i<2*num_ffe_taps; i++) {
    tmp_freq_ffe[i]   = ffe[i];
  }
  for (i=0; i<2*num_dfe_taps; i++) {
    tmp_freq_dfe[2+i] = dfe[i];
  }
  
  // do an FFT on both arrays 
  fft(tmp_freq_ffe-1, DURATION, 1); 
  fft(tmp_freq_dfe-1, DURATION, 1); 

  // determine combined response 
  for (i=0; i<DURATION; i++) {
  	tmpNum = complex_mag(tmp_freq_ffe[2*i],tmp_freq_ffe[2*i+1]);
    tmpDen = complex_mag(tmp_freq_dfe[2*i],tmp_freq_dfe[2*i+1]);
		if (tmpDen != 0.0 && tmpNum != 0.0)  // avoid division by zero
    	freq[i] = 20.0 * log10(tmpNum / tmpDen);
    else
    	freq[i] = 0.0; 
  }

  // swap frequency order 
  for (i=0; i<DURATION; i++)   
  	tmp_freq_ffe[i] = freq[DURATION-i-1];
  for (i=0; i<DURATION; i++) {
    if (i >= (DURATION/2)) freq[i] = tmp_freq_ffe[i - DURATION/2];
    else                   freq[i] = tmp_freq_ffe[i + DURATION/2];
  }

  free(tmp_freq_ffe);
  free(tmp_freq_dfe);
}


/**************************************************************
* equ_monitor REVB - This routine monitors the performance of the
* QAM Equalizer. The screen is divided into 4 sections ...
* section 1 - real-time plot of real component of equalizer coefficients
* section 2 - imag-time plot of real component of equalizer coefficients
* section 3 - list of statistics computed from equalizer data
* section 4 - either frequency response of equalizer of real-time plot
*             of equalizer error.
* This routine samples the tap weights each pass. The HC11 acquisition is
* not used for the tap plots. The equalizer error plot uses it.
* 1/8/94 - PJM -
**************************************************************/
int equ_monitor_b(void)
{
  int      q, gdriver = DETECT, gmode, errorcode;
  int      xsiz,ysiz;
  int      x1,x2,y1,y2,xctr,yctr;
  int      i,j,c,x,done=0;
  char     msg[80];
  int      section0[4],section1[4],section2[4],section3[4],section4[4];
  int      fkey_win[4];
  int      xdim,ydim;
  int      nbytes,nwords,newdata,ffe,dfe;
  int      ffe_taps[32],dfe_taps[32];
  int      ffe_tmp[32],dfe_tmp[32];
  int      ffe_mag[32] ,dfe_mag[32];
  int      hi,ilo,qlo,i_err,q_err,refresh;
  int      main_tap;
  float    offset;
  float    xscale,yscale,hscale,correction;
  float    *freq_response,mag;
  static float  plotbuf[BUFSIZE];
  int      lval1, lval2;
  float    avg_err,snr,fval1,fval2,err_scale,rms_nonlin;
  char     snr_str[32];
  char     *title[4],title_buf[80];
  static int initialized=0;
  static int runstat=1;      /* 1=running, 0=stopped  */
  static int log_scale=0;    /* 1=log scale,0=linear  */
  /* static */ int auto_scale;     /* 0=fixed scale for snr */
  char     *lb_ptr[20],listbuf[16][32],scalebuf[32];
  long     err_sum;
  int      do_fft_calc;
  int      sample_rate;
  int      show_freq=0;
  int      snr_index=0;

  char *fkeys[] = {
    ""    ,
    ""    ,
    "View Taps"     ,
    "Log Tap Scale" ,
    "Stop"          ,
    " "             ,

    "Freq Response" , 
    ""    ,
    "Reacquire"    ,
    "Exit"          ,
  };

  enum equ_status {
	 	LOCK_STATE=0 ,
	 	SEL_QAM_MODE ,
	 	SNR_ESTIMATE ,
  };
  int nstatus = 3;
  plot_status_t status[] = {
    "Lock Status "  , ""       , ""  ,
    "QAM Mode"  , ""       , ""  ,
    "SNR Estimate"  , ""       , "" ,
  };

  freq_response = malloc(DURATION*sizeof(float));
  
  for (i=0; i<64; i++) snr_data[i] = 0.0;

  /* read the qam status */
  qam_read_status();

  /* determine starting menu settings */
  fkeys[3] = (log_scale)     ? "Linear Scale"  : "Log Scale" ;
  fkeys[4] = (runstat)       ? "Stop"          : "Run"       ;

  /* init vars is 1st call */
  if (!initialized || runstat) {
    for (i=0; i<BUFSIZE; i++) plotbuf[i] = 0;
    initialized = 1;
  }

  /* init graphics mode and get screen size */
  initgraph(&gdriver, &gmode, "");
  if ((errorcode = graphresult()) != grOk) {
    printf("Graphics error: %s\n", grapherrormsg(errorcode));
    getch();
    exit(1);
  }
  xsiz = getmaxx();
  ysiz = getmaxy();

  /* now determine the coordinates of each sub-section */
  xdim = (xsiz - 7*EQU_BORDER) / 2;
  ydim = (ysiz - 15*EQU_BORDER) / 2;
  section0[0] = section0[1] = EQU_BORDER;
  section0[2] = xsiz - EQU_BORDER;
  section0[3] = ysiz - EQU_BORDER;
  /* equalizer taps section */
  section1[0] = section1[1] = 3*EQU_BORDER;
  section1[2] = section1[0] + (xdim*5/4);
  section1[3] = section1[1] + ydim;
  
  /* SNR stripchart section */
  section2[0] = 3*EQU_BORDER;
  section2[1] = section1[3] + 2*EQU_BORDER;
  section2[2] = section2[0] + xdim;
  section2[3] = section2[1] + ydim;
  
  /* Equalizer Statistics section */
  section3[0] = section2[2] + 8*EQU_BORDER;
  section3[1] = 3*EQU_BORDER;
  section3[2] = section3[0] + xdim;
  section3[3] = section3[1] + ydim;
  
  /* Frequency response section */
  section4[0] = section2[2] + 2*EQU_BORDER;
  section4[1] = section3[3] + 2*EQU_BORDER;
  section4[2] = section4[0] + xdim;
  section4[3] = section4[1] + ydim;
  fkey_win[0] = 2*EQU_BORDER;
  fkey_win[2] = xsiz - 2*EQU_BORDER;
  fkey_win[1] = ysiz - 2*EQU_BORDER - FKEY_MENU_HEIGHT;
  fkey_win[3] = ysiz - 2*EQU_BORDER;
  fkey_menu(1,fkey_win, fkeys);

  refresh = (runstat) ? 0 : 1;

  while (!done) {
		if (runstat || refresh) {
      /* draw the border */
      refresh = 0;
      setlinestyle(SOLID_LINE,0xffff,THICK_WIDTH);
      setcolor(YELLOW);
      rectangle(section0[0],section0[1],section0[2],section0[3]);
      settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
      settextjustify(CENTER_TEXT,CENTER_TEXT);
      outtextxy(xsiz/2,EQU_BORDER/2,"QAM Adaptive Equalizer Performance Monitor");

      /* draw the section border */
      setcolor(WHITE);
      setlinestyle(SOLID_LINE,0,NORM_WIDTH);
      rectangle(section1[0],section1[1],section1[2],section1[3]);
      rectangle(section2[0],section2[1],section2[2],section2[3]);
      rectangle(section4[0],section4[1],section4[2],section4[3]);
      outtextxy(section1[0]+xdim/2,section1[1]-10,"Magnitude of Equalizer Taps");

      /* sample the equalizer taps */
      if (runstat) {
        for (i=0; i<8; i++) {
          qam_read(MB_R,MB_LDF0+i,&ffe_taps[2*i],&ffe_taps[2*i+1]);
          qam_read(MB_R,MB_LDD0+i,&dfe_taps[2*i],&dfe_taps[2*i+1]);
        }
        /* read remaining 4 DFE taps, these aren't back-to-back with 1st 8 */
        for (i=0; i<4; i++) {
          qam_read(MB_R,MB_LDD8+i,&dfe_taps[2*(8+i)],&dfe_taps[2*(8+i)+1]);
        }
    	}

    	/* compute the tap magnitude */
    	for (i=0; i<8; i++) {
        mag = (float) ffe_taps[2*i] * ffe_taps[2*i] + (float) ffe_taps[2*i+1] * ffe_taps[2*i+1];
        mag = (mag > 1.0e-06) ? sqrt(mag) : 0.0;
        if (mag > 32767.0) mag = 32767.0;
        ffe_mag[i] = (int) mag;
      }

    	for (i=0; i<12; i++) {
        mag = (float) dfe_taps[2*i] * dfe_taps[2*i] + (float) dfe_taps[2*i+1] * dfe_taps[2*i+1];
        mag = (mag > 1.0e-06) ? sqrt(mag) : 0.0;
        if (mag > 32767.0) mag = 32767.0;
        dfe_mag[i] = (int) mag;
	    }

  	  /* determine scaling */
    	xscale = 0.5 * (xdim - 2*EQU_BORDER - 8) / 8;
    	if (log_scale)  
      	yscale = 1.0 * (ydim - EQU_BORDER) / (80.0);   /* -60 dB to 20 dB */
      else          
      	yscale = 1.0 * (ydim - EQU_BORDER) / (8.0);

      /* draw hash marks */
      setfillstyle(SOLID_FILL,0);
      bar(1+section1[0],1+section1[1],section1[2]-1,section1[3]-1);
      xctr = section1[0] + xdim/2 + EQU_BORDER/2;
      yctr = section1[3] - EQU_BORDER;
      setcolor(RED);
      setlinestyle(USERBIT_LINE,0x3333,NORM_WIDTH);
      if (log_scale) {
        hscale = (ydim - EQU_BORDER) / 8.0;
        for (i=0; i<8; i++) 
        	line(section1[0],yctr-hscale*i,section1[2],yctr-hscale*i);
      } else {
        hscale = (ydim - EQU_BORDER) / 8.0;
        for (i=0; i<8; i++) 
        	line(section1[0],yctr-hscale*i,section1[2],yctr-hscale*i);
      }
      setlinestyle(SOLID_LINE,0,NORM_WIDTH);
      
    	/* display the taps */
	 		for (i=0; i < 12; i++) {   /* DFE Equalizer */
      	setfillstyle(SOLID_FILL,1+(i%8));
      	x1 = xctr + 2 + i*xscale;
      	x2 = xctr + 2 + i*xscale + xscale;
				y2 = yctr;
      	if (log_scale) {
		  		if (dfe_mag[i] < 5) 
          	y1 = yctr;
		  		else 
          	y1 = 1.0 * yctr - yscale * (60.0 + 20.0 * log10((float) dfe_mag[i] / 4096.0));
      	} else y1 = 1.0 * yctr - yscale * dfe_mag[i] / 4096.0;
				if (y1 > y2) 
        	y1=y2;
				bar(x1,y1,x2,y2);
    	}
      
    	for (i=0; i < 8; i++) {   /* FFE Equalizer */
				setfillstyle(SOLID_FILL,1+(i%8));
      	x1 = xctr - 2 - (7-i)*xscale - xscale;
      	x2 = xctr - 2 - (7-i)*xscale;
      	y2 = yctr;
      	if (log_scale) {
					if (ffe_mag[i] < 5) y1 = 1.0 * yctr;
					else y1 = 1.0 * yctr - yscale * (60.0 + 20.0 * log10((float) ffe_mag[i] / 4096.0));
      	} else y1 = 1.0 * yctr - yscale * ffe_mag[i] / 4096.0;
      	if (y1 > y2) y1=y2;
      	bar(x1,y1,x2,y2);
    	}
    	setcolor(WHITE);
    	line(section1[0],yctr,section1[2],yctr);
    	line(xctr,section1[1],xctr,section1[3]);

    	/* label the axis */
    	outtextxy(section1[0]+xdim/4  ,section1[1]+EQU_BORDER,"FFE");
    	outtextxy(section1[0]+3*xdim/4,section1[1]+EQU_BORDER,"DFE");

    	/* display the scale factors */
    	setcolor(WHITE);
    	settextstyle(SMALL_FONT,HORIZ_DIR,2);
    	settextjustify(LEFT_TEXT,CENTER_TEXT);
    	if (log_scale) {
      	hscale = (ydim - EQU_BORDER) / 8;
      	for (i=1; i<8; i++) {
					sprintf(scalebuf,"%4.1fdB",10.0*i-60.0);
					outtextxy(2+section1[0],xctr-i*hscale,scalebuf);
      	}
    	} else {
      	hscale = (ydim - EQU_BORDER) / 8;
      	for (i=1; i<8; i++) {
					sprintf(scalebuf,"%6.1f",1.0*i);
					outtextxy(2+section1[0],xctr-i*hscale,scalebuf);
      	}
    	}
    	settextstyle(SMALL_FONT,HORIZ_DIR,2);
    	settextjustify(RIGHT_TEXT,CENTER_TEXT);
    	if (log_scale) {
      	hscale = (ydim - EQU_BORDER) / 8;
      	for (i=1; i<8; i++) {
        	sprintf(scalebuf,"%4.1fdB",10.0*i-78.06);
        	outtextxy(section1[2]-2,xctr-i*hscale,scalebuf);
      	}
    	} else {
      	hscale = (ydim - EQU_BORDER) / 8;
      	for (i=1; i<8; i++) {
        	sprintf(scalebuf,"%5.3f",0.125*i);
        	outtextxy(section1[2]-2,xctr-i*hscale,scalebuf);
      	}
    	}
    	settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

      /* this gets updated with the qam_read_status() routine */
    	snr_buf[snr_index++] = qam_status.snr_estimate;
        
      if (snr_index >= 16) {
        for (i=0; i<48; i++) snr_data[i] = snr_data[i+16];
        for (i=0; i<16; i++) snr_data[48+i] = snr_buf[i];
        snr_index = 0;
        title[0] = "SNR Estimate";
        title[1] = "Time ->";
        title[2] = "dB";
        line_plot(section2, snr_data, 64, 50.0, PLOT_UNARY, title);
      }

      /* Read FFECTL register to determine who is the main tap */
      qam_read(SB_R, SB_FFECTL, &i, &i);
      switch( i & 0x38 ) {
        case 0x00:  /* position F0 */	 main_tap = 0;	break;
        case 0x08:  /* position F1 */  main_tap = 1;  break;               
        case 0x10:  /* position F2 */  main_tap = 2;	break;               
        case 0x18:  /* position F3 */  main_tap = 3;	break;               
        case 0x20:  /* position F4 */  main_tap = 4;	break;               
        case 0x28:  /* position F5 */  main_tap = 5;	break;               
        case 0x30:  /* position F6 */  main_tap = 6;	break;               
        case 0x38:  /* position F7 */  main_tap = 7;	break;               
      }        
      
      /* calculate the linearized equalizer freq. response   */
      /* do this only if main tap is greater than all others */
      if (show_freq) {
        show_freq = 0;
        do_fft_calc = 1;
        for (i=0; i<8; i++) {
          if (ffe_mag[i] > ffe_mag[main_tap]) 
          	do_fft_calc = 0;
        }
        for (i=0; i<12; i++) {
          if (dfe_mag[i] > ffe_mag[main_tap]) 
          	do_fft_calc = 0;
        }
        if (do_fft_calc) {
                ffe = 8;
                dfe = 12;
                equ_freq_response(ffe_taps,dfe_taps,freq_response,ffe,dfe);
          title[0] = "Equalizer Freq. Response";
          sprintf(title_buf,"Carrier Frequency +/- 2.5 MHz");
          title[1] = title_buf;
          title[2] = "Gain(dB)";
          line_plot(section4,freq_response,DURATION,20.0,PLOT_SPECTRUM,title);
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -