-
Notifications
You must be signed in to change notification settings - Fork 551
/
FIR_data_gen.m
executable file
·144 lines (112 loc) · 6.18 KB
/
FIR_data_gen.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
% Copyright (C) 2023, Advanced Micro Devices, Inc. All rights reserved.
% SPDX-License-Identifier: MIT
%-------------------------------------------------------------------------------
% FIR Filter Data Generator for Tutorial XD061
% Real Part is Sine Wave
% Imaginary Part is Impulse
%
% User Parameters:
% interval : number of 1 unit intervals in the data set
% samples : number of data samples
% r_amplitude: wave amplitude
% r_freq: : freqency (in unit intervals)
% i_amplitude: wave amplitude
% i_freq: : freqency (in unit intervals)
%
% Real part is sine wave; Complex is Unit Impulse
%
% All calculations are for cint16
%
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
% User Parameters:
%-------------------------------------------------------------------------------
interval = 4
samples = 8192
r_amplitude = 16383
r_freq = 2
i_amplitude = 16383
i_freq = 4
fir_coefs = [ 255 511 767 1023 1279 1535 1791 2047 1791 1535 1279 1023 767 511 255]
%fir_coefs = [ 1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 18 19 20 21 22 ...
% 23 24 25 27 28 29 30 31 32 33 34 36 37 38 39 40 41 42 44 45 ...
% 46 47 48 49 50 51 53 54 55 56 57 58 59 60 62 63 64 65 66 67 ...
% 68 69 71 72 73 74 75 76 77 78 80 81 82 83 84 85 86 88 89 90 ...
% 91 92 93 94 95 97 98 99 100 101 102 103 104 106 107 108 109 110 111 112 ...
% 113 115 116 117 118 119 120 121 122 124 125 126 127 128 129 130 132 133 134 135 ...
% 135 134 133 132 130 129 128 127 126 125 124 122 121 120 119 118 117 116 115 113 ...
% 112 111 110 109 108 107 106 104 103 102 101 100 99 98 97 95 94 93 92 91 ...
% 90 89 88 86 85 84 83 82 81 80 78 77 76 75 74 73 72 71 69 68 ...
% 67 66 65 64 63 62 60 59 58 57 56 55 54 53 51 50 49 48 47 46 ...
% 45 44 42 41 40 39 38 37 36 34 33 32 31 30 29 28 27 25 24 23 ...
% 22 21 20 19 18 16 15 14 13 12 11 10 9 7 6 5 4 3 2 1 ]
n_fir_filters = 1
fir_downshift = 14
%-------------------------------------------------------------------------------
% Input Data Computations:
%-------------------------------------------------------------------------------
fir_downscale = 2 ^ fir_downshift
t=(0:samples-1)
data_in = zeros(2, samples)
data_in(1,:) = round(r_amplitude * sin (2 * pi * r_freq * t * interval / samples))
data_in(2,:) = i_amplitude * (mod(t, round(samples/(i_freq*interval))) == 0)
%-------------------------------------------------------------------------------
% Write Input File for AIESimulator:
%-------------------------------------------------------------------------------
fileID = fopen('AIESim_Input_Data.txt', 'w')
%fprintf(fileID, " %6d %6d\n", data_in)
data_in_x4 = reshape(data_in, 8, samples/4)
fprintf(fileID, " %6d %6d %6d %6d %6d %6d %6d %6d\n", data_in_x4)
fclose(fileID)
%-------------------------------------------------------------------------------
% Write Input File for Host Application:
%-------------------------------------------------------------------------------
fileID = fopen('input_data.h', 'w')
fprintf(fileID, "// This data header file automatically generated by FIR_data_gen.m\n")
fprintf(fileID, "// r_amplitude: %5d r_freq: %5d\n", r_amplitude, r_freq)
fprintf(fileID, "// i_amplitude: %5d i_freq: %5d\n\n", i_amplitude, i_freq)
fprintf(fileID, "#define INPUT_SAMPLES %5d\n\n", samples)
fprintf(fileID, "cint16 input_data[] = {\n")
fprintf(fileID, " {%6d, %6d},\n", data_in(:,1:samples-1))
fprintf(fileID, " {%6d, %6d}\n", data_in(:,samples))
fprintf(fileID, "};\n\n")
fclose(fileID)
%-------------------------------------------------------------------------------
% Output Data Computations:
%-------------------------------------------------------------------------------
for filt = 1:n_fir_filters
data_out = zeros(2, samples)
data_out(1,:) = floor(conv(fir_coefs, data_in(1,:))(1:samples) / fir_downscale)
data_out(2,:) = floor(conv(fir_coefs, data_in(2,:))(1:samples) / fir_downscale)
data_in = data_out
end
%-------------------------------------------------------------------------------
% Write Output File for AIESimulator:
%-------------------------------------------------------------------------------
fileID = fopen('AIESim_Golden_Data.txt', 'w')
fprintf(fileID, "# This data file automatically generated by FIR_data_gen.m\n")
fprintf(fileID, "# interval : %5d\n", interval)
fprintf(fileID, "# samples : %5d\n", samples)
fprintf(fileID, "# n_fir_filters: %5d\n", n_fir_filters)
fprintf(fileID, "# fir_downshift: %5d\n", fir_downshift)
fprintf(fileID, "# fir_coefs : [ %s ]\n\n", num2str(fir_coefs))
fprintf(fileID, " %6d %6d\n", data_out(:,1:samples))
fclose(fileID)
%-------------------------------------------------------------------------------
% Write Output File for Host Application:
%-------------------------------------------------------------------------------
fileID = fopen('golden_data.h', 'w')
fprintf(fileID, "// This data header file automatically generated by FIR_data_gen.m\n")
fprintf(fileID, "// interval : %5d\n", interval)
fprintf(fileID, "// samples : %5d\n", samples)
fprintf(fileID, "// n_fir_filters: %5d\n", n_fir_filters)
fprintf(fileID, "// fir_downshift: %5d\n", fir_downshift)
fprintf(fileID, "// fir_coefs : [ %s ]\n\n", num2str(fir_coefs))
fprintf(fileID, "#define GOLDEN_N_FIR_FILTERS %3d\n", n_fir_filters)
fprintf(fileID, "#define GOLDEN_N_FIR_TAPS %3d\n", numel(fir_coefs))
fprintf(fileID, "#define OUTPUT_SAMPLES %5d\n\n", samples);
fprintf(fileID, "cint16 golden_data[] = {\n")
fprintf(fileID, " {%6d, %6d},\n", data_out(:,1:samples-1))
fprintf(fileID, " {%6d, %6d}\n", data_out(:,samples))
fprintf(fileID, "};\n\n")
fclose(fileID)