Skip to content

Commit b00346a

Browse files
committed
Adding LT model for filter module
1 parent a83f679 commit b00346a

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#ifndef IPS_FILTER_LT_MODEL_HPP
2+
#define IPS_FILTER_LT_MODEL_HPP
3+
#ifdef IPS_DEBUG_EN
4+
#include <iostream>
5+
#endif // IPS_DEBUG_ENi
6+
#ifdef IPS_DUMP_EN
7+
#include <sstream>
8+
#endif // IPS_DUMP_EN
9+
#include <systemc.h>
10+
11+
12+
/**
13+
* @brief Filter module.
14+
* It takes care of filtering a image/kernel using a median filter or an
15+
* equivalent convolution like:
16+
* | 1/N^2 ... 1/N^2 | | img(row - N/2, col - N/2) ... img(row + N/2, col + N/2) |
17+
* img(row, col) = | ... ... .... | * | ... ... ... |
18+
* | 1/N^2 ... 1/N^2 | | img(row + N/2, col - N/2) ... img(row + N/2, col + N/2) |
19+
*
20+
* @tparam IN - data type of the inputs
21+
* @tparam OUT - data type of the outputs
22+
* @tparam N - size of the kernel
23+
*/
24+
template <typename IN = sc_uint<8>, typename OUT = sc_uint<8>, uint8_t N = 3>
25+
SC_MODULE(Filter)
26+
{
27+
protected:
28+
//----------------------------Internal Variables----------------------------
29+
#ifdef IPS_DUMP_EN
30+
sc_trace_file* wf;
31+
#endif // IPS_DUMP_EN
32+
OUT* img_window_tmp;
33+
OUT* kernel;
34+
OUT* result_ptr;
35+
36+
// Event to trigger the filter execution
37+
sc_event event;
38+
39+
//-----------------------------Internal Methods-----------------------------
40+
void exec_filter();
41+
void init();
42+
43+
public:
44+
/**
45+
* @brief Default constructor for Filter
46+
*/
47+
SC_HAS_PROCESS(Filter);
48+
#ifdef IPS_DUMP_EN
49+
/**
50+
* @brief Construct a new Filter object
51+
*
52+
* @param name - name of the module
53+
* @param wf - waveform file pointer
54+
*/
55+
Filter(sc_core::sc_module_name name, sc_core::sc_trace_file* wf)
56+
: sc_core::sc_module(name), wf(wf)
57+
#else
58+
/**
59+
* @brief Construct a new Filter object
60+
*
61+
* @param name - name of the module
62+
*/
63+
Filter(sc_core::sc_module_name name) : sc_core::sc_module(name)
64+
#endif // IPS_DUMP_EN
65+
{
66+
// Calling this method by default since it is no time consumer
67+
// It is assumed that this kernel is already loaded in the model
68+
// Kernel does not change after synthesis
69+
SC_METHOD(init);
70+
// Thread waiting for the request
71+
SC_THREAD(exec_filter);
72+
}
73+
74+
//---------------------------------Methods---------------------------------
75+
void filter(IN* img_window, OUT* result);
76+
};
77+
78+
/**
79+
* @brief Execute the image filtering
80+
*
81+
*/
82+
template <typename IN, typename OUT, uint8_t N>
83+
void Filter<IN, OUT, N>::exec_filter()
84+
{
85+
size_t i;
86+
size_t j;
87+
88+
while (true)
89+
{
90+
// Wait to peform the convolution
91+
wait(this->event);
92+
93+
// Default value for the result depending on the output datatype
94+
*(this->result_ptr) = (OUT) 0;
95+
96+
// Perform the convolution
97+
for (i = 0; i < N; ++i)
98+
for (j = 0; j < N; ++j)
99+
*(this->result_ptr) += this->kernel[i * N + j] * this->img_window_tmp[i * N + j];
100+
}
101+
}
102+
103+
/**
104+
* @brief Filtering image
105+
*
106+
* @param img_window - image window to filter
107+
* @param result - resultant pixel
108+
*/
109+
template <typename IN, typename OUT, uint8_t N>
110+
void Filter<IN, OUT, N>::filter(IN* img_window, OUT* result)
111+
{
112+
size_t i;
113+
size_t j;
114+
115+
// Default value for the result depending on the output datatype
116+
this->result_ptr = result;
117+
118+
// Perform the convolution
119+
for (i = 0; i < N; ++i)
120+
for (j = 0; j < N; ++j)
121+
this->img_window_tmp[i * N + j] = (OUT) img_window[i * N + j];
122+
123+
// N * N * copy_pixel_to_mem_time + mult + redux + copy_pixel_to_mem_time
124+
// Image is copied pixel by pixel
125+
const float DELAY_TIME = (N * N * 1) + 4 + 2 + 1;
126+
this->event.notify(DELAY_TIME, SC_NS);
127+
}
128+
129+
/**
130+
* @brief Initializes a kernel of N x N with default value of 1 / (N^2)
131+
*
132+
*/
133+
template <typename IN, typename OUT, uint8_t N>
134+
void Filter<IN, OUT, N>::init()
135+
{
136+
// Init a kernel of N x N with default value of 1 / (N * N)
137+
this->kernel = new OUT[N * N];
138+
std::fill_n(this->kernel, N * N, ((OUT) 1) / ((OUT) N * N));
139+
// Init image window of N x N with default value of 1 / (N * N)
140+
this->img_window_tmp = new OUT[N * N];
141+
#ifdef IPS_DEBUG_EN
142+
// Print the initialized kernel
143+
SC_REPORT_INFO(this->name(), "init result");
144+
size_t i, j;
145+
146+
for (i = 0; i < N; ++i)
147+
{
148+
for (j = 0; j < N; ++j)
149+
{
150+
std::cout << "[" << this->kernel[i * N + j] << "]";
151+
152+
#ifdef IPS_DUMP_EN
153+
// Adding the signals to the waveform
154+
std::ostringstream var_name;
155+
var_name << "kernel_" << i << "_" << j;
156+
sc_trace(this->wf, this->kernel[i * N + j], var_name.str());
157+
#endif // IPS_DUMP_EN
158+
}
159+
160+
std::cout << std::endl;
161+
}
162+
#endif // IPS_DEBUG_EN
163+
}
164+
#endif // IPS_FILTER_LT_MODEL_HPP

modules/filter/src/tb_filter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ void run_one_window()
8686
// Instantiate filter module and do the connection
8787
#ifdef IPS_DUMP_EN
8888
Filter<IPS_IN_TYPE_TB, IPS_OUT_TYPE_TB, IPS_FILTER_KERNEL_SIZE> filter("filter", wf);
89+
sc_trace(wf, result, "result");
8990
#else
9091
Filter<IPS_IN_TYPE_TB, IPS_OUT_TYPE_TB, IPS_FILTER_KERNEL_SIZE> filter("filter");
9192
#endif // IPS_DEBUG_EN
@@ -98,14 +99,19 @@ void run_one_window()
9899
#endif // IPS_DEBUG_EN
99100

100101
// Apply convolution
102+
#ifdef IPS_FILTER_PV_EN
101103
filter.filter(img_window, result);
104+
#elif defined(IPS_FILTER_LT_EN)
105+
filter.filter(img_window, &result);
106+
sc_start(100, SC_NS);
107+
#endif // IPS_FILTER_XX_EN
108+
102109
#ifdef IPS_DEBUG_EN
103110
SC_REPORT_INFO("TEST_MODE_ONE_WINDOW", "filtering");
104111
std::cout << "Result = " << result << std::endl;
105112
#endif // IPS_DEBUG_EN
106113

107114
#ifdef IPS_DUMP_EN
108-
sc_trace(wf, result, "result");
109115
sc_start(1, SC_NS);
110116
#endif // IPS_DUMP_EN
111117

0 commit comments

Comments
 (0)