coef_seq.cpp

来自「vhdl source,ver-fir-coefficient,simulink」· C++ 代码 · 共 947 行 · 第 1/2 页

CPP
947
字号

// Copyright (C) 1991-2004 Altera Corporation
// Any megafunction design, and related net list (encrypted or decrypted),
// support information, device programming or simulation file, and any other
// associated documentation or information provided by Altera or a partner
// under Altera's Megafunction Partnership Program may be used only to
// program PLD devices (but not masked PLD devices) from Altera.  Any other
// use of such megafunction design, net list, support information, device
// programming or simulation file, or any other related documentation or
// information is prohibited for any other purpose, including, but not
// limited to modification, reverse engineering, de-compiling, or use with
// any other silicon devices, unless such use is explicitly licensed under
// a separate agreement with Altera or a megafunction partner.  Title to
// the intellectual property, including patents, copyrights, trademarks,
// trade secrets, or maskworks, embodied in any such megafunction design,
// net list, support information, device programming or simulation file, or
// any other related documentation or information provided by Altera or a
// megafunction partner, remains with Altera, the megafunction partner, or
// their respective licensors.  No other licenses, including any licenses
// needed under any third party's intellectual property, are provided herein.
// Company : Altera Corp
// Filename : coef_seq.cpp
// Last Update : Nov 17, 2004
// Description:
// 	This open source C/C++ code is for creating the input coefficient sequences
//      for use with FIR filters generated by the FIR Compiler MegaCore
//
//
// The command to use coef_seq.exe is:
// coef_seq.exe <path to the input coefficient file> \
// input_file.txt <path to the output coefficient file> \
// output_file.txt <FIR structure> <coefficient store> \
// <allow or disallow symmetry> <number of calculations for MCV>  \
// <coefficient bit width for others> <number of coefficient sets> \
// <filter rate> <filter factor> <coefficient bit width>

// where:                                                                  
// <FIR structure> is                                                   
// MCV - multi-cycle variable                                            
// SER - fully serial                                                    
// MBS - multi-bit serial                                                
// PAR - fully parallel                                                  
//
// <coefficient store> is                                               
// LC - logic cells                                                      
// M512 - M512 blocks                                                    
// M4K - M4K blocks                                                      
//
// <allow or disallow symmetry> is:                                     
// MSYM - Take advantage of symmetric coefficients                       
// NOSYM - Use non-symmetric coefficients                                
//
// <number of calculations for MCV/ coefficient bit width for others> is:
// for multi-cycle variable filters, the number of clock cycles to      
// calculate the result or all other filters, use the coefficient bit width                 
//
// <filter rate> is be specified as one of the following (SGL, INT, DEC)
// SGL - Single Rate FIR Filter                                          
// INT - Interpolating FIR Filter                                        
// DEC - Decimating FIR Filter                                           
//
// <filter factor> is an integer value representing the rate-changing factor.                                                                 
// For single-rate filters, this argument should be set to 1.           
// For multi-rate FIR filters, this argument should be an integer between 1z and 16.  

// NOTE : The entire command line must be provided in a single line with no carriage \
// returns or line-feeds
//
// NOTE : For additional information on how to use this program, please refer to the
// FIR Compiler User Guide



//coef_seq.cpp : Defines the entry point for the console application.
//


//#include "stdafx.h"
#include <iostream>
#include <fstream.h>
#include <stdio.h>
#include <math.h>

enum coef_store_type {LC, M512, M4K};
enum struct_type {MCV, SER, MBS, PAR};
enum sym_chk {NOSYM,MSYM};
enum poly_type {SGL,INT,DEC};


void itobi(char str_deci[32], int i)
{
//	char str_deci[60];
	char str_temp[32];

	str_deci[0] = '\0';
	int med;
	while (i > 0)
	{
		str_temp[0] = '\0';
		int j= strlen(str_temp);
		med = (i >> 1);
		if ( i == (med << 1) )
		{strcat(str_temp,"0");}
		else
		{strcat(str_temp,"1");}
		i = med;
		strcat(str_temp,str_deci);
		strcpy(str_deci,str_temp);
	}

//	return str_deci;

}



int main(int argc, char* argv[])
{
	int coef[2048];
	int tmp_coef[2048];
	int coef_ori[2048];
	int coef_mr[2048];
	int coef_mr_arr[128][16];
	int mcv_coef_length;
	int zeros_insert;
	int num_coef_set;
	int poly_fact;
	int coef_bit_width;
	int coef_set_width = 0;
	int coef_width;

	int coef_length;
	int coef_length_ori = 0;
	int sym = 0;
	long num_cycles = 1;
	int num_mac;
	int i,j,k,l,m,jg;

	char in_name[256];
	char out_name[256];
	char *non;
	enum coef_store_type coef_store_type;
	enum struct_type struct_type;
	enum sym_chk sym_chk;
	enum poly_type poly_type;

	char temp_char[20];
	char temp[32];
	char bi_index[256];

	int num_coef_group;
	int mem_width;
	int width_stage[6];
	int group_size =0;
	int flag = 0;
	int width_index;

	float max_value = pow(2.0,36);// initial as m4k
	int width_index_ini = 5;
	int group_size_ini = 7 - coef_set_width;
	int index_cnt;
	int sub_coef[30];
	int size;


	int length_wr = 0;
	int ai = 0;
	int bi = 0;	
	int ci = 0;	
				

	ofstream out_file;
	ofstream temp_file;

	if (argc >= 11 ) // if the user calls this program with no arguments ...
	{

		int arg_err = 0;
		strcpy(in_name,argv[1]);// input file name, full path
		strcpy(out_name,argv[2]);// out file name, full path
		
		strcpy(temp_char, argv[3]);//structure type
		if( strcmp(temp_char, "MCV") == 0)
		{
			struct_type = MCV;
		}
		else if( strcmp(temp_char, "SER") == 0)
		{
			struct_type = SER;
		}
		else if( strcmp(temp_char, "MBS") == 0)
		{
			struct_type = MBS;
		}
		else if( strcmp(temp_char, "PAR") == 0)
		{
			struct_type = PAR;
		}
		else
		{
 			printf("ERROR of argv[3]:  Structure Type should be one of the following:MCV,SER,MBS,PAR\n");
			arg_err = 1;
		}
		strcpy(temp_char, argv[4]);//coef_storage type
		if( strcmp(temp_char, "LC") == 0)
		{
			coef_store_type = LC;
		}
		else if( strcmp(temp_char, "M512") == 0)
		{
			coef_store_type = M512;
		}
		else if( strcmp(temp_char, "M4K") == 0)
		{
			coef_store_type = M4K;
		}
		else
		{
			printf("ERROR of argv[4]:    Coef store type should be one of the following:LC M512,M4K\n");
			arg_err = 1;
		}

		strcpy(temp_char, argv[5]);//NOSYM
		if( strcmp(temp_char, "NOSYM") == 0)
		{
			sym_chk = NOSYM;
		}
		else if( strcmp(temp_char, "MSYM") == 0)
		{
			sym_chk = MSYM;
		}
		else
		{
			printf("ERROR of argv[5]:    Pluse use NOSYM/MSYM\n");
			arg_err = 1;
		}

		if(struct_type == MCV)
		{
			num_cycles = strtol(argv[6], &non,10);//num of cycles
			if(num_cycles < 0 )
			{
				arg_err = 1;
				printf("ERROR of argv[6]:    num of calculation cycles should be positive\n");
			}
		}
		else
		{
			coef_bit_width = strtol(argv[6], &non,10);//num of coef_bit_width
			if(coef_bit_width < 0  || coef_bit_width > 32)
			{
				arg_err = 1;
				printf("ERROR of argv[6]:    coefficient bit width should be positive and less than 32\n");
			}
		}
		num_coef_set = strtol(argv[7], &non,10);//num of coefficient set
		if(num_coef_set < 0  || num_coef_set > 16)
		{
			arg_err = 1;
			printf("ERROR : argv[7] :    number of coefficient sets should be positive and less than 17\n");
		}

		strcpy(temp_char, argv[8]);//poly_type
		poly_type = SGL;
		if( strcmp(temp_char, "SGL") == 0)
		{
			poly_type = SGL;
		}
		else if( strcmp(temp_char, "INT") == 0)
		{
			poly_type = INT;
		}
		else if( strcmp(temp_char, "DEC") == 0)
		{
			poly_type = DEC;
		}
		else
		{
			printf("ERROR : argv[8]:    Filter rate description should be one of the following:SGL INT DEC\n");
			arg_err = 1;
		}


		poly_fact = strtol(argv[9], &non,10);//multi-rate factor
		if(poly_fact < 0  || poly_fact > 16)
		{
			arg_err = 1;
			printf("ERROR : argv[9]:    Multi rate factor should be an integer in the range 1 to 16\n");
		}
		else
		{
			if(poly_fact>1)
			{
				num_coef_set = num_coef_set * poly_fact;
			}
		}
		
		coef_width = strtol(argv[10], &non,10);//coefficient width
                if(coef_width < 0  || coef_width > 32)
                {
                        arg_err = 1;
                        printf("ERROR : argv[10]:    Coefficient width should be an integer in the range 2 to 32\n");
                }


		fstream in_file;
		in_file.open(in_name, ios::in);
		if(in_file.is_open() == 0)
		{
			printf("ERROR argv[1]:  Can not open %s\n", argv[1]);
		}

		for (i = 0; i<2048; ++i)
		{
			coef[i] = 0;
		}

		j=0;
		i=0;
		double float_coef;
		char coef_line[256];
		while (!in_file.eof())
		{
			strcpy(coef_line, "\n");
 			in_file >>coef_line;
			if(coef_line[0] != '\n')
			{
				coef[i] = atoi(coef_line);
				float_coef = atof(coef_line);
				if(coef[i] != float_coef)
				{
					printf("ERROR argv[1]: %s can only contain fixed point numbers.\n", argv[1]);
				}
				++i;
			}
		}
/*		if(coef[i-1] != double_coef[i-1])
		{
		--i;
		}*/
//		--i;

		if(i == 0)
		{
			printf("ERROR argv[1]:  %s is empty.\n", argv[1]);
		}

		coef_length = i;

		sym = 1;
		 int half_len = ceil((coef_length +0.)/2.0);
		 for (i = 0; i < half_len; ++i)
		 {
			if(coef[i] != coef[coef_length-1-i])
			{
				sym = 0;
			}
		 }
		 if(sym == 0)
		 {
			sym = -1;
			 for (i = 0; i < half_len; ++i)
			 {
				if(coef[i] != -coef[coef_length-1-i])
				{
					sym = 0;
				}
			 }
		 }

		 if(sym_chk == NOSYM)
		 {
			 sym = 0;
		 }

		in_file.close();

		if(struct_type == MCV && num_cycles > ((int) ceil((double) coef_length)/2.0))
		{
//			printf("ERROR argv[6]:  number of calculation cycles should less than half of number of coefficients\n", argv[6]);
//			arg_err = 1;
		}

		if(arg_err == 0)
		{
		    if(struct_type == MCV )
			{
				int mcv_reload_zero_insert = 0;
				int coef_one_mem = 0;
				int mem_num = 0;
				if(sym != 0 && poly_type == SGL)
				{coef_length = half_len;}
				num_mac = ceil((coef_length + 0.0)/(num_cycles+0.0));
				mcv_coef_length = num_mac * num_cycles;
				if(coef_store_type == M512)
				{
					coef_one_mem = (int) floor(18.0/((double) coef_width));
				}
				else if(coef_store_type == M4K)
				{
					coef_one_mem = (int) floor(36.0/((double) coef_width));
				}
				else
				{
					coef_one_mem = 1;
				}

				mem_num = (int) ceil(((double) num_mac)/ ((double)coef_one_mem));
				mcv_reload_zero_insert = mem_num * coef_one_mem - num_mac;
				if(mem_num == 1)
				{
					mcv_reload_zero_insert = 0;
				}


				if(sym == 0)
				{
					zeros_insert = 0;
				}
				else
				{
//					zeros_insert = floor((mcv_coef_length - coef_length)/2.0);
					zeros_insert = floor((mcv_coef_length - coef_length));
				}

				for (i=0; i<2048; ++i)
				{
					tmp_coef[i] = 0;
					coef_ori[i] = coef[i];
				}

/*				if(sym == 1)
				{
					coef_length = ceil(coef_length/2.0);
					mcv_coef_length = ceil(mcv_coef_length/2.0);
					num_mac = ceil(num_mac/2.0);
				}*/

				for (i=0; i<coef_length; ++ i)
				{
					tmp_coef[i + int(zeros_insert)] = coef[i];
				}

				for (i=0; i<mcv_coef_length; ++ i)
				{
					coef[i] = tmp_coef[i];
				}

				int k = 0;

				for (j=0; j<num_mac; ++j)
				{
					for (i=0; i<num_cycles; ++i)
					{
						k = i*num_mac + j;
						if (i==0)
						{
							tmp_coef[k] = coef[(num_cycles - 1) * num_mac + j];
						}
						else
						{
							tmp_coef[k] = coef[(i -1 ) * num_mac + j];
						}
					}
				}

				for (j=0; j<num_mac; ++j)
				{
					for (i=0; i<num_cycles; ++i)
					{
						k = i*num_mac + (num_mac - 1 - j);
						if(coef_store_type == LC)
						{
							coef[j*num_cycles + i] = tmp_coef[k];

⌨️ 快捷键说明

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