# Implementing a FIR Filter

**URL:** <https://www.ruby-forum.com/t/implementing-a-fir-filter/243059>\
**Category:** GNU Radio\
**Created:** [August 18, 2015, 10:27pm UTC](https://www.ruby-forum.com/t/implementing-a-fir-filter/243059 "2015-08-18T22:27:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [August 18, 2015, 10:27pm UTC](https://www.ruby-forum.com/t/implementing-a-fir-filter/243059/1 "2015-08-18T22:27:38Z")

</div>

Hi all,

Would someone please recommend a good starting point in C++ source code  
that demonstrates how one uses the kernel::fir\_filter. I started  
following  
the pfb\_clock\_sync method of implementing it, but then realized this had  
been customized a bit for the polyphase approach.

I’m not doing anything fancy, I’m building a modulator and need to pass  
symbols through a shaping filter. The shaping filter is what I need the  
FIR  
filter for.

Appreciated,  
Rich

---

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [August 19, 2015, 12:36am UTC](https://www.ruby-forum.com/t/implementing-a-fir-filter/243059/2 "2015-08-19T00:36:09Z")

</div>

Rich,

hilbert\_fc.cc and filter\_delay\_fc both use kernel::fir\_filter in a  
relatively easy to understand way.

Jeff

---

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [August 19, 2015, 9:06pm UTC](https://www.ruby-forum.com/t/implementing-a-fir-filter/243059/3 "2015-08-19T21:06:29Z")

</div>

Thank you Jeff. Those are perfect.

Rich

---

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [August 20, 2015, 2:00pm UTC](https://www.ruby-forum.com/t/implementing-a-fir-filter/243059/4 "2015-08-20T14:00:01Z")

</div>

On Wed, Aug 19, 2015 at 2:17 PM, Richard B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> kernel::fir_filter_ccf *d_shape_filter;
> 
> ```
> 
> To my CMakeLists.txt file I added FILTER to the dependency list:  
> set(GR\_REQUIRED\_COMPONENTS RUNTIME FILTER)
> 
> I’m not sure where to go from here. What else needs to happen to call  
> kernel::fir\_filter\_ccf?
> 
> Thanks,  
> Rich

Probably a namespace issue. You’ve included the fir\_filter.h, I’m  
assuming.  
Now, instead of referencing it as “kernel::fir\_filter\_ccf” use the full  
namespace “gr::filter::kernel::fir\_filter\_ccf”.

Tom

---

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [August 20, 2015, 10:17pm UTC](https://www.ruby-forum.com/t/implementing-a-fir-filter/243059/5 "2015-08-20T22:17:45Z")

</div>

I have a fundamental misunderstanding of the filter system in gnuradio.  
I  
would appreciate some help. I wrote a simple C++ testbench to help me  
figure out what’s going on, but it continues to not behave as I expect.  
I  
am using fir\_filter\_fff with 29 taps that I set. I send an impulse into  
the  
filter and want to see the filter taps at the output in response. The  
first  
sample that comes out is correct, but every sample after the first is  
either the smallest or largest number representable by float. The code  
that  
does this is below.

float impulse[29] =  
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // 29  
samples  
long

float filter\_taps[29] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1,  
1,  
1, 1, 1, 1, 1, 1, 1, 1, 1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1}  
//  
input to filter

shape\_filter = new gr::filter::kernel::fir\_filter\_fff(1, filter\_taps);

for(int i = 0; i \< 29; i++) {  
filter\_out[i] = shape\_filter-\>filter(&impulse[i]);  
}

Printing filter\_out to the screen produces:  
0.1 0 8.12753e-44 1.61149e-43 -4.16205e-20 -8.32411e-20  
-1.24862e-19 -1.66482e-19 1.13358e+21

When I expect to see  
0.1 0.2 0.3 0.4 etc…, the taps of the filter that I set above

You can see only the first output matches the expected.

_What am I doing wrong here?_

Thanks,  
Rich

On Wed, Aug 19, 2015 at 12:02 PM, Richard B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [August 20, 2015, 2:05pm UTC](https://www.ruby-forum.com/t/implementing-a-fir-filter/243059/6 "2015-08-20T14:05:52Z")

</div>

That fixed the problem. Thank you.

Rich

---

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [August 21, 2015, 1:53am UTC](https://www.ruby-forum.com/t/implementing-a-fir-filter/243059/7 "2015-08-21T01:53:20Z")

</div>

I got this working. For completeness, I’ll answer my own questions and  
sum  
up what I’ve learned about filtering.

1. You need to call set\_history(num\_filter\_taps) so that block  
boundaries  
are handled smoothly
2. You input samples one at a time to fir\_filter\_xxf
3. fir\_filter\_xxf does not store data in it’s own memory space, it uses  
the  
address of the array element you give it to figure out the rest of the  
data  
it should use to process around it, this is why history is necessary

The last problem I was having was related to converting an array of char  
to  
float before feeding it into the filter. Not realizing how  
fir\_filter\_xxf  
figured out what data to use, I was typecasting each char to float  
sample  
by sample as the loop progressed and assigning the single float to a new  
variable that was not an array. I then passed the address of this  
variable  
to filter. This would cause the filter to use values in addresses around  
that variable in its calculations, leading to ridiculous results. Don’t  
do  
this!

Rich

On Thu, Aug 20, 2015 at 12:37 PM, Richard B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![reback](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@reback](https://www.ruby-forum.com/u/reback)\
**Post date:** [August 21, 2015, 2:03am UTC](https://www.ruby-forum.com/t/implementing-a-fir-filter/243059/8 "2015-08-21T02:03:16Z")

</div>

I’m having some trouble getting my code to compile. I get the following  
error:

error: ‘kernel’ does not name a type  
kernel::fir\_filter\_ccf \*d\_shape\_filter;

To my \*\_impl.h file I added the following:  
#include \<gnuradio/filter/fir\_filter.h\>

private:  
kernel::fir\_filter\_ccf \*d\_shape\_filter;

To my CMakeLists.txt file I added FILTER to the dependency list:  
set(GR\_REQUIRED\_COMPONENTS RUNTIME FILTER)

I’m not sure where to go from here. What else needs to happen to call  
kernel::fir\_filter\_ccf?

Thanks,  
Rich

On Wed, Aug 19, 2015 at 9:39 AM, Richard B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
