📄 coef_seq.cpp
字号:
// 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.
// <coefficient bit width> is the integer value representing the userspecified
// coefficient bit width, which ranges from 2-32.
// 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 <iostream>
#include <fstream>
#include <stdio.h>
#include <math.h>
using namespace std;
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};
char out_name[256];
int coef[2048];
int coef_ori[2048];
enum coef_store_type coef_store_type;
enum struct_type struct_type;
enum sym_chk sym_chk;
enum poly_type poly_type;
int poly_fact;
int num_coef_set;
int coef_bit_width;
int coef_length;
int coef_length_ori = 0;
int sym = 0;
long num_cycles = 1;
int num_mac;
void mcv_single_rate_reseq();
void distru_reseq();
void multi_rate_poly_mcv_reseq();
void deci_reseq();
void single_rate_out_write();
void multi_rate_poly_out_write();
void deci_out_write();
void itobi(char[32], int);
int num_coef_group;
int mcv_coef_length;
int group_size = 0;
int mem_width;
int length_wr;
int mcv_reload_zero_insert;
int main(int argc, char* argv[])
{
int coef_set_width = 0;
// int coef_width;
int i,j;
char in_name[256];
char *non;
char temp_char[20];
int flag = 0;
float max_value = (float)pow(2.0,36);// initial as m4k
int width_index_ini = 5;
int group_size_ini = 7 - coef_set_width;
ofstream out_file;
// ofstream temp_file;
int arg_err = 0;
/***********************************************************************************************
// Parameters check
/*********************************************************************************************/
if (argc >= 11 ) // if the user calls this program with no arguments ...
{
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_type == DEC && struct_type == MCV)
{
}
else if(poly_fact>1)
{
num_coef_set = num_coef_set * poly_fact;
}
}
coef_bit_width = strtol(argv[10], &non,10);//coefficient width
if(coef_bit_width < 0 || coef_bit_width > 32)
{
arg_err = 1;
printf("ERROR : argv[10]: Coefficient width should be an integer in the range 2 to 32\n");
}
ifstream in_file(in_name, ios::in);
//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_line[0] != 0)
{
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(i == 0)
{
printf("ERROR argv[1]: %s is empty.\n", argv[1]);
}
coef_length = i;
for (i=0; i<2048; ++ i)
{
coef_ori[i] = coef[i];
}
sym = 1;
int half_len = (int)ceil(((double)coef_length)/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 == 0 && sym_chk == MSYM)
{
printf("The coefficients are non_symmetric.\n", argv[6]);
arg_err = 1;
}
if(sym_chk == NOSYM)
{
sym = 0;
}
in_file.close();
/********************End of parameters check*****************************/
}
else
{
printf("Error: This program requires 10 arguments! \n ");
}
if(arg_err == 1)
{
printf("Error: Parameters is not right!!! \n ");
}
else
{
if(poly_type == SGL)
{
if(struct_type == MCV)
{
mcv_single_rate_reseq();
}
else
{
distru_reseq();
}
single_rate_out_write();
}
// else if(poly_type == DEC && sym != 0)
else if(poly_type == DEC && struct_type == MCV)
{
deci_reseq();
deci_out_write();
}
else if(poly_type == INT || poly_type == DEC)
{
if(struct_type == MCV)
{
multi_rate_poly_mcv_reseq();
}
else
{
distru_reseq();
}
multi_rate_poly_out_write();
}
}
return 0;
}
void mcv_single_rate_reseq()
{
/****************************MCV FIR resequence Single rate************************/
int tmp_coef[2048];
int coef_one_mem = 0;
int mem_num = 0;
int half_len = (int)ceil(((double) coef_length)/2.0);
int zeros_insert;
int i,j,k;
if(struct_type == MCV )
{
if(sym != 0 && (poly_type == SGL || poly_type == DEC))
{coef_length = half_len;}
num_mac = (int) ceil(((double) coef_length)/((double) num_cycles));
int mcv_coef_length = num_mac * num_cycles;
if(coef_store_type == M512)
{
coef_one_mem = (int) floor(18.0/((double) coef_bit_width));
if(coef_one_mem == 0)
{
coef_one_mem = 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -