Convert Matlab SOS filters to C#

Converts Matlab SOS filters into a fast C# class
1.2K Downloads
Updated 7 Dec 2010

View License

This file takes a filter in sos sections and converts it into a C# class. It "unwinds" the sos stages explicitly without looping for speed...it runs faster under managed C# than it does under Matlab.

For example, to design a sos filter:

H = design(fdesign.highpass('fst,fp,ast,ap',250, 300, 20, 3, 1000),'cheby1','MatchExactly', 'passband');

Then to convert the sos sections into C# code:
sos2csharp(H,'filter.cs')

This creates the C# class filter.cs file in the current working directory. Add it to your C# solution. To call it within C#, for instance,

double[] x {1.0, 2.0, 4.0, 2.0}; // input data
double[] y = new double[4]; // output data
Filter myFilter = new Filter();
myFilter(x, y); // the filtered data is now in array y
double[] x1 {4.0, 2.0, 7.0, 1.0}; // new input data
myFilter(x1, y); // the initial conditions are saved, so this is
// same as if you gave it the concatenation of [x x1]

Cite As

James Squire (2025). Convert Matlab SOS filters to C# (https://www.mathworks.com/matlabcentral/fileexchange/23530-convert-matlab-sos-filters-to-c), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Audio Processing Algorithm Design in Help Center and MATLAB Answers
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.2.0.0

Clarified example calling code

1.1.0.0

Added example of calling it from within a C# program

1.0.0.0