5)hamming_window.cpp程序分析
hamming_window.cpp
[cpp] view plaincopy
#include "hamming_window.h" // Provides default WINDOW_LEN if not user defined
// Translation module function prototypes:
static void hamming_rom_init(in_data_t rom_array[]);
// Function definitions:
void hamming_window(out_data_t outdata[WINDOW_LEN], in_data_t indata[WINDOW_LEN])
{
static in_data_t window_coeff[WINDOW_LEN];
unsigned i;
// In order to ensure that 'window_coeff' is inferred and properly
// initialized as a ROM, it is recommended that the array initialization
// be done in a sub-function with global (wrt this source file) scope.
hamming_rom_init(window_coeff);
for (i = 0; i < WINDOW_LEN; i++)
{
#pragma HLS UNROLL
outdata = (out_data_t)window_coeff * (out_data_t)indata;
}
}
// This initialization function will be optimized away during high level
// sythesis (HLS), resulting in the underlying memory being inferred as a ROM
// by RTL synthesis.
static void hamming_rom_init(in_data_t rom_array[WINDOW_LEN])
{
int i;
for (i = 0; i < WINDOW_LEN; i++)
{
#pragma HLS PIPELINE
float real_val = 0.54f -
0.46f * cos(2.0f * M_PI * i / (float)(WINDOW_LEN - 1));
rom_array = (in_data_t)(WIN_COEFF_SCALE * real_val);
}
}
hamming_window.h
[cpp] view plaincopy
#ifndef HAMMING_WINDOW_H_
#define HAMMING_WINDOW_H_
#include
#include
#include
#include
#include
using namespace std;
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// If WINDOW_LEN is not defined by the user, define a default value
#ifndef WINDOW_LEN
#define WINDOW_LEN 256
#endif
// If WIN_COEFF_FRACBITS not defined by user, set a default value
#ifndef WIN_COEFF_FRACBITS
#define WIN_COEFF_FRACBITS (14)
#endif
// Set the scale factor for the window coefficients
#define WIN_COEFF_SCALE ((float)(1 << WIN_COEFF_FRACBITS))
// This function applies an Hamming window function to the 'indata' buffer,
// returning the windowed data in 'outdata'. The coefficients are 16-bit
// scaled integer, which may be interpreted as a signed fixed point format,
// with WIN_COEFF_FRACBITS bits after the binary point.
//typedef int16_t in_data_t;
//typedef int32_t out_data_t;
#include "ap_int.h"
typedef ap_int<16> in_data_t;
typedef ap_int<32> out_data_t;
void hamming_window(out_data_t outdata[], in_data_t indata[]);
#endif // HAMMING_WINDOW_H_ not defined
6)hamming_window.cpp程序分析
testbench用于测试hamming_window模块的正确性。本例选取的600个采样点进行测试,数据为:从0到+75,再到-75,再到+75,再到-75,以此类推,间隔为1.把fir的结果和一个标准结果文件out.gold.dat进行比较,如果相同,说明程序正确。
[cpp] view plaincopy
#include
#include"hamming_window.h"
int main(int argc,char*argv[])
{
in_data_t test_data[WINDOW_LEN];
out_data_t hw_result[WINDOW_LEN], sw_result[WINDOW_LEN];
int i;
unsigned err_cnt =0, check_dots =0;
for(i =0; i < WINDOW_LEN; i++){
// Generate a test pattern for input to DUT
test_data=(in_data_t)((32767.0*(double)((i %16)-8)/8.0)+0.5);
// Calculate the coefficient value for this index
in_data_t coeff_val =(in_data_t)(WIN_COEFF_SCALE *(0.54-
0.46* cos(2.0* M_PI * i /(double)(WINDOW_LEN -1))));
// Generate array of expected values -- n.b. explicit casts to avoid
// integer promotion issues
sw_result=(out_data_t)test_data*(out_data_t)coeff_val;
}
// Call the DUT
printf("Running DUT...");
hamming_window(hw_result, test_data);
printf("done.\n");
// Check the results returned by DUT against expected values
printf("Testing DUT results");
for(i =0; i < WINDOW_LEN; i++){
if(hw_result!= sw_result){
err_cnt++;
check_dots =0;
cout <<
cout <<"\n!!! ERROR at i ="<< i <<"- expected:"<< sw_result<<"got:"<< hw_result<< endl;
}else{// indicate progress on console
if(check_dots ==0)
printf("\n");
printf(".");
if(++check_dots ==64)
check_dots =0;
}
}
printf("\n");
// Print final status message
if(err_cnt){
printf("!!! TEST FAILED - %d errors detected !!!\n", err_cnt);
}else
printf("*** Test Passed ***\n");
// Only return 0 on success
return err_cnt;
}
3. 导出IP
选择export RTL,导出格式选择IP Catalog。