Skip to content

Commit

Permalink
修改自动生成C代码的命令,并修改以满足在MacOS下编译
Browse files Browse the repository at this point in the history
  • Loading branch information
uingrd committed Feb 10, 2022
1 parent c28a422 commit 895c68c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ CONV/export_code/test_conv_1d
CONV/export_code/test_fir
GNB/export_code/test
CSD/export_code/test
FXP/test
FXP/y_int.bin
*.pyc
FXP/fxp_filter_data.c
FXP/fxp_filter_data.c
18 changes: 15 additions & 3 deletions FXP/fxp_filter.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <stdio.h>
#include <malloc.h>
#include <stdint.h>
#include <stdlib.h>

#ifndef CLANG_MACOS
#include <malloc.h>
#endif

extern const int NUM_TAPS; // 滤波器抽头数目
extern const int Y_SHIFT; // 输出数据格式转换移位量
Expand All @@ -27,18 +31,26 @@ int8_t fir(int8_t x)
return (int8_t)sum;
}

void main()
int main()
{
FILE* fp;
int8_t y;
buf=(int8_t*)calloc(NUM_TAPS,1);
# ifdef CLANG_MACOS
fp = fopen("y_int.bin", "wb");
#else
fopen_s(&fp, "y_int.bin", "wb");
#endif

buf=(int8_t*)calloc(NUM_TAPS,1);

for (int n=0; n<NUM_X; n++)
{
y=fir(x_int[n]);
fwrite(&y,1,1,fp);
}
fclose(fp);
free(buf);

return 0;
}

15 changes: 11 additions & 4 deletions FXP/fxp_filter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#coding:utf-8
#!/usr/bin/python

import os, platform, IPython
import os, sys, platform, IPython
import numpy as np
from scipy.signal import kaiserord, lfilter, firwin, freqz

Expand All @@ -10,6 +10,9 @@

np.random.seed(1234)

# 设置当前运行目录
os.chdir(sys.path[0])

####################
# 演示对通过定点数运算实现FIR近似滤波
####################
Expand Down Expand Up @@ -141,8 +144,12 @@ def find_fxp_fmt(v,num_bits):
f.write('\n};\n\n')

# 编译并运行C测试程序
os.system('gcc -o fxp_filter fxp_filter_data.c fxp_filter.c -std=c99')
os.system('fxp_filter')
if platform.system()=='Darwin': # MacOS
os.system('clang -o test fxp_filter_data.c fxp_filter.c -DCLANG_MACOS -std=c99')
os.system('./test')
else:
os.system('gcc -o test fxp_filter_data.c fxp_filter.c -std=c99')
os.system('./test')

# 读回C测试程序输出的定点数
fxp_y_f32_c=np.fromfile('y_int.bin', dtype=np.int8).astype(float)/(2.0**mbits_y)
Expand All @@ -152,6 +159,6 @@ def find_fxp_fmt(v,num_bits):
plt.plot(fxp_y_f32,'r')
plt.plot(fxp_y_f32_c-fxp_y_f32,'k')
plt.title('fxp comparison, C vs. python')
plt.legend(['C output','fxp_y', 'quant. err.'])
plt.legend(['C output','fxp_y', 'err.'])
plt.show()

14 changes: 7 additions & 7 deletions FXP/fxp_filter_data.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const int Y_SHIFT = 12;
const int Y_SHIFT = 10;
const int NUM_TAPS = 85;
const int NUM_X = 1000;
const signed char x_int[] =
Expand Down Expand Up @@ -70,11 +70,11 @@ const signed char x_int[] =

const signed char taps_int[] =
{
18, 19, 19, 19, 19, 18, 17, 15, 12, 10, 7, 3, 0, -3, -7, -10,
-13, -16, -19, -21, -22, -23, -24, -24, -23, -22, -20, -17, -15, -11, -8, -4,
0, 4, 8, 12, 15, 18, 21, 23, 24, 25, 26, 25, 24, 23, 21, 18,
15, 12, 8, 4, 0, -4, -8, -11, -15, -17, -20, -22, -23, -24, -24, -23,
-22, -21, -19, -16, -13, -10, -7, -3, 0, 3, 7, 10, 12, 15, 17, 18,
19, 19, 19, 19, 18,
9, 9, 10, 10, 9, 9, 8, 7, 6, 5, 3, 2, 0, -2, -3, -5,
-7, -8, -9, -10, -11, -12, -12, -12, -12, -11, -10, -9, -7, -6, -4, -2,
0, 2, 4, 6, 7, 9, 10, 11, 12, 13, 13, 13, 12, 11, 10, 9,
7, 6, 4, 2, 0, -2, -4, -6, -7, -9, -10, -11, -12, -12, -12, -12,
-11, -10, -9, -8, -7, -5, -3, -2, 0, 2, 3, 5, 6, 7, 8, 9,
9, 10, 10, 9, 9,
};

0 comments on commit 895c68c

Please sign in to comment.