Tag Archives: fir

FIR filtering on the Raspberry Pi

hbdigi-proto-raspberry

A HiFiBerry user asked, if it would be possible to use BruteFIR for room equalization with the DAC. I was pretty sure that BruteFIR only runs on Intel CPUs. Looks like I was wrong. It seems, that BruteFIR also runs on the Raspberry Pi. Alternatively JConvolver also implements FIR filtering on the Raspberry. One commenter noted, that he has a setup running BruteFIR with 32k taps and 50% CPU load on the Raspberry. Wow! This is something we have to look into. Using the HiFiBerry Digi with an additional SPDIF input would allow to use the Raspberry Pi as a flexible room-equalization module.

Has anybody implemented a setup like this already? Let us know!

Comparisson IIR and FIR filters

There are lots of discussion in the DIY loudspeaker community, if digital loudspeaker crossover filters should be designed using IIR (infinite impulse response) or FIR (finite impulse response) filters. As IIR-capable DSPs are usually cheaper, many people use them.

This short article should give some information about advantages and disadvantages of both filter types.

Comparison of IIR and FIR filters

IIR FIR
 Feedback  yes  no
 Latency  low  high
 Processing complexity  low  high
 Comparable to analog circuits  yes  no
 Linear phase filtering  no  yes

One argument often used pro FIR and contra IIR filters is the phase response. It is possible, to design FIR filters, that change the frequency response, but not the phase response – so-called linear phase filters. However, have a look at real circuits and even a loudspeaker. No real circuit with capacitors or inductors used for filtering is a linear-phase system. Also a loudspeaker chassis changes its phase response depending on the frequency. Even in high-end speakers, passive LC-crossovers introduce phase shifts. It seems, that a changing phase response is not the worst thing in the world.

There are two major disadvantages of FIR filters: the need a lot of processing power and they introduce a longer delay than IIR filters. Depending on the hardware used, the first issue might not be a disadvantage. But the delay can be a problem in many use cases.



Are FIR filters bad? No, but you should understand the differences of IIR and FIR filters and then decide what kind of filtering you need. For loudspeaker crossovers, IIR filters often work well – as the old school passive filters did before.

References

HiFiBerry DSP – processing power

Raspberry Pi and HiFiBerry DSP Light

While we’re working on the DSP software, we had a closer look at the performance of the used ADAU1701. At 48kHz Sample rate, this DSP can process 1024 command per sample. With a normal CPU, that would not be a lot. But what about this DSP? Let’s have a look: a single-precision biquad filter needs 6 commands, a double-precision filter 10 commands. That means with single precision filters we would be able to use more than 150 biquad filters and even with double-precision filters there are about 100 filters possible. For a simple loudspeaker crossover, that’s a lot.

But what about FIR filtering? FIR filters are quite resource-intense. Their computing complexity is linearly dependent on the number of filter taps. The ADAU1701 can process one tap per command. That means you could process FIR filters up to about 1000 taps length on this DSP. This might not be enough for FIR based room correction, but enough for a FIR-based crossover for a normal 2 way speaker. We will see, if we can implement this.

A simple XML model for filter networks

For the HiFiBerry DSP project we need a compact, but flexible configuration of a network of filters. Therefore I created an XML configuration that models the different parts of the filter network.

<network samplerate="48000">
<input name="input1"/>
<biquad name="bq1" input="input1" type="allpass" frequency="200" q="1" />
<biquad name="bq2" input="bq1" type="lowpass" frequency="1000" q="0.7" />
<biquad name="bq3" input="bq2" type="highpass" frequency="100" q="0.7" />
<biquad name="bq4" input="bq3" type="notch" frequency="400" q=".1" />
<biquad name="bq5" input="bq4" type="peaking_eq" frequency="700" q="4" dbgain="3" />
<output name="out1" input="bq5" />
</network>

Here you see a simple DSP configuration with one input and one output and 5 biquad filters processing the data. Today, no other filters are supported, but I will add other types soon. I plan to also support FIR filters.

Using our software you can now calculate the frequency response of this filter chain:

out1
  Freq       Mag   Phase
    10  -40.27db  151.26°
    15  -33.54db  137.31°
    20  -28.95db  123.77°
    30  -22.92db   98.07°
    40  -19.11db   73.96°
    50  -16.52db   51.02°
    75  -13.06db   -3.32°
   100  -12.19db  -54.31°
   150  -13.86db -148.08°
   200  -16.87db -232.90°
   300  -24.77db -340.74°
   400 -298.86db -480.46°
   500  -26.91db -242.30°
   750  -16.50db -310.78°
  1000  -16.52db -346.21°
  1500  -17.48db -391.49°
  2000  -19.60db  -58.47°
  3000  -23.71db  -89.23°
  4000  -27.23db -107.29°
  5000  -30.26db -119.52°
  7500  -36.41db -137.98°
 10000  -41.23db -148.30°
 15000  -48.77db -159.52°
 20000  -54.92db -165.64°

Now let’s do a nice graph from it using Matplotlib.
frequency-response-demo
Note, this is not the filter that we used before, because the response of that filter is really nasty – it was just for demonstration of the different biquad filters.

Now, we only have to merge this with our DSP upload software with this code and we already have a simple command line tool to upload and modify filters on the HiFiBerry DSP.

HiFIBerry DSP Light – progress report

 

There was still a minor bug in the input section of the HiFiBerry DSP Light prototype. It was just a resistor that was soldered only on one side. After fixing this bug, the board it now working correctly. That means we have a DSP board that can be programmed from the Raspberry Pi and has analog inputs and outputs. I usually do the basic tests using a signal generator and the oscilloscope. However, a simple sine wave is a boring subject for a DSP. Simple biquad filters do not modify the waveform. But let’s see, what happens if we use the abs() function on a sine wave:

abs

Oops, the signal is inverted. But this is not an issue, inverting a signal in the DSP is a trivial function.

That means we can now focus on the software development. The first step is the EEPROM functionality. With the onboard EEPROM the DSP works even without the Raspberry Pi (however without the option to control it over the network).

The next step will be a universal filter designer that can be used for crossovers, equalizing and even room equalization. The software will be open source. Are you interested to join the software development team? Contact us!

 

Changing the output filter characteristics of the HiFiBerry Mini DAC

hifiberry-filter-jumperThe HiFiBerry Mini has a cool feature: you can switch the characteristics of the output filter by software. The DAC chip generates high output frequencies. These have to be filtered to remove all components above the audio bandwidth. There are two parts of this filtering process: a digital filter on the DAC chip itself and a first-order analog filter. You can see the analog filter between the two RCA connectors on the port.

But let’s have a look on the digital filter. It can run in IIR and in FIR mode. There are a lot of discussion, which is better. The good thing here is: you can listen to both and find out be yourself what you like more.

There is a jumper “Filter” on the board. It can be used like this:

  • no jumper – DAC runs in IIR mode (low latency)
  • jumper left – DAC runs in FIR mode (higher latency)
  • jumper right – filter can be controlled by the Raspberry Pi

But how do you control the filter settings from the Raspberry Pi? You just have to control GPIO27 of the Raspberry.

WiringPi

A nice program to control the state of the GPIO ports is the WiringPi toolset.

sudo gpio export 27 out
sudo gpio readall
+----------+-Rev2-+------+--------+------+-------+
| wiringPi | GPIO | Phys | Name   | Mode | Value |
+----------+------+------+--------+------+-------+
|      0   |  17  |  11  | GPIO 0 | IN   | Low   |
|      1   |  18  |  12  | GPIO 1 | IN   | Low   |
|      2   |  27  |  13  | GPIO 2 | OUT  | Low   |
|      3   |  22  |  15  | GPIO 3 | IN   | Low   |
|      4   |  23  |  16  | GPIO 4 | IN   | Low   |
|      5   |  24  |  18  | GPIO 5 | IN   | Low   |
|      6   |  25  |  22  | GPIO 6 | IN   | Low   |
|      7   |   4  |   7  | GPIO 7 | IN   | Low   |
|      8   |   2  |   3  | SDA    | OUT  | Low   |
|      9   |   3  |   5  | SCL    | ALT0 | High  |
|     10   |   8  |  24  | CE0    | ALT0 | High  |
|     11   |   7  |  26  | CE1    | ALT0 | High  |
|     12   |  10  |  19  | MOSI   | ALT0 | Low   |
|     13   |   9  |  21  | MISO   | ALT0 | Low   |
|     14   |  11  |  23  | SCLK   | ALT0 | Low   |
|     15   |  14  |   8  | TxD    | ALT0 | High  |
|     16   |  15  |  10  | RxD    | ALT0 | High  |
|     17   |  28  |   3  | GPIO 8 | IN   | Low   |
|     18   |  29  |   4  | GPIO 9 | IN   | High  |
|     19   |  30  |   5  | GPIO10 | IN   | Low   |
|     20   |  31  |   6  | GPIO11 | IN   | Low   |
+----------+------+------+--------+------+-------+

You can now toggle the port using

sudo gpio write 2 1 # low latency IIR filter
sudo gpio write 2 0 # normal latency FIR filter

Shell script

If you don’t want to install additional software, you can also use a shell script:

sudo echo "27"  > /sys/class/gpio/export
sudo echo "out" > /sys/class/gpio/gpio27/direction 
sudo echo "1" > /sys/class/gpio/gpio27/value # low latency IIR filter
sudo echo "0" > /sys/class/gpio/gpio27/value # normal latency FIR filter
Link

firfilterOne of our readers sent me a link to a filter design site: T-Filter. It specialized in the calculation of FIR filters. I had a quick look at it and it looks nice. The filter calculation was very fast. But then I noticed that I was only calculating a filter for 2000 Hz sampling frequency. This is not really a real-world audio calculation. Then I tried 48 kHz sample rate. This also work, but the default passband ripple of 5 db is way too much for HiFi audio applications. Reducing the ripple to 0.5 db often led to empty filter. But the software is still in beta, therefore it may work in the future.