# Matlab SOS Filter in C++

I'm struggling to validate that my C++ implementation of a filter I designed in Matlab is correct.

My first issue is to make sure I'm implementing the sections in the right order. This is the output of the SOS matrix from fdatool:

0.5961   -1.1815    0.5961    1.0000   -1.9792    0.9884
0.5793   -1.1475    0.5793    1.0000   -1.9555    0.9649
0.6200   -1.2266    0.6200    1.0000   -1.9300    0.9398
0.5658   -1.1167    0.5658    1.0000   -1.9013    0.9119
0.4348   -0.8542    0.4348    1.0000   -1.8689    0.8806
0.3066   -0.5967    0.3066    1.0000   -1.8330    0.8459
0.1814   -0.3451    0.1814    1.0000   -1.7960    0.8104
0.0735   -0.1285    0.0735    1.0000   -1.7637    0.7794
0.0168   -0.0087    0.0168    1.0000   -1.7443    0.7609


I'm assuming that the sections should be processed from top to bottom, is that correct?

My second question is to do with scaling. I was scaling my filters using scale(Hd) directly, but I'm noticing that I get inconsistent results in my program. I have generated multiple filters that should output roughly the same data but the peak values are quite different. I noticed there is a ScaleValues property on the filter object, after scaling the values are mostly 1.0:

0.1047
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000


I was all ready to assume these are applied to the output of each section, then I noticed there is 1 more scaling value than section. How are these values used?

Final question, given the options to scale, not to scale, various ways to scale, if I'm targeting a 64-bit CPU or maybe GPU doing my math in double-precision floats, is there an optimal way to configure scaling?

• "I'm assuming that the sections should be processed from top to bottom, is that correct?" The order doesn't matter. No matter which order the filter stages are cascaded, they will produce the same output. I guess there could be some numerical precision issues, but they should be negligible for double-precision and 2nd-order sections – endolith Dec 6 '13 at 18:31

The ScaleValues property is easy to incorporate. Multiply the entire input signal by the first scale value, then the output of each section by subsequent values.