Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why a1, a2 of SOS are always inverse sign compare to matlab results? #9

Open
yangbo opened this issue Nov 2, 2023 · 2 comments
Open

Comments

@yangbo
Copy link

yangbo commented Nov 2, 2023

Hi ruohoruotsi, Thanks for your work on this library.

I found the Butterworth class's output a1 and a2 of SOS are awayls change sign compare to matlab results.
Such as if c++ output a1=0.1,a2=-0.2 then matlab's a1=-0.1, a2=0.2.

Is there something that I missing? If the output of Butterworth.bandPass() gives digital filter Biquad coefficients then is it should be the
same as matlab's results?

Thanks for your help.

C++ Test code like below:

int filterOrder = 4;
double gain = 1.0;
double Fs = 100;
double fp1 = 0.5;
double fs1 = 10;

vector <Biquad> coeffs;  // second-order sections (sos)
Butterworth butterworth;

bool designedCorrectly = butterworth.bandPass(Fs,             // fs
                                            fp1,            // freq1
                                            fs1,            // freq2. N/A for lowpass
                                            filterOrder,
                                            coeffs,
                                            gain);

Matlab test code like:

  order = 4; 
  Fs=100;     
  fp1=0.5;    
  fs1=10;     
  % design digital Butter-Worth-Filter in SOS form
  [z,p,k]=butter(order,[fp1*2/Fs,fs1*2/Fs],'bandpass');
  [bsos,bk] = zp2sos(z,p,k);

Then you can see coeffs[i].a1 == -bsos[i+1, 5] and coeffs[i].a2 == -bsos[i+1, 6]. i from 0 to length of coeffs.

@ruohoruotsi
Copy link
Owner

A good question that I'll need to get into the code to debug why.

  • Especially if the difference is a sign, my instinct is to look at whether the Direct Form is correct.
  • I also have MATLAB folder in the repo where I have generated all my unit tests "ground truth".

As you can tell I haven't been active here much, but there seem to be some small bugs popping up that I need to fix/improve. I'll have a look this weekend.

Also, please feel free to make a PR from your fork, I appreciate your contributions and code contributions 🙇 🙏

@yangbo
Copy link
Author

yangbo commented Nov 4, 2023

Thanks for your reply.

I have added some features in my fork of this project.

They are:

  1. Added sos2tf() function to convert SOS to Transfer Function coefficients like matlab's SOS2TF.
  2. Added conv() function to do convolution.
  3. Added CMake as building system, and move tests to 'tests/' subdirectory. Because I do not use SConstruct so I didn't change it, but changed the doc to cmake.
  4. Added some cross-compile support for arm64 target, but they are specific to my development environment, so I don't know if I should include them in PR.

I will try to make a PR, if there are something wrong, please tell me. : )

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants