2

Matlab zp2sos returns [sos, g] but in python equivalent library zpk2sos returns only sos. How to calculate gain g in python.

ts=(length(data1)/128);
Wp = [1 4]/(fs/2); Ws = [0.5 4.5]/(fs/2);
Rp = 3; Rs = 40;
[n,Wn] = buttord(Wp,Ws,Rp,Rs);
[z, p, k] = butter(n,Wn,'bandpass');
[sos,g] = zp2sos(z,p,k);
filt = dfilt.df2sos(sos,g);

I'm converting this Matlab code to Python but can't find the library required to calculate gain g returned by Matlab zp2sos function.

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
  • The title and most of the question are about `g`, but in the last sentence you ask a separate question about `dfilt.df2sos`. I think you should create a new stackoverflow question for that. One question per topic keeps the issue focused and easier to answer. – Warren Weckesser Jul 13 '18 at 17:59
  • @WarrenWeckesser Thanks. Edited the question. – MD. Khairul Basar Jul 13 '18 at 18:15

1 Answers1

1

When you use the form

[sos,g] = zp2sos(z,p,k);

g is the same as k.

For example,

> [sos, g] = zp2sos([-1 0.5 0.6], [-0.5 0.1+0.25j 0.1-0.25j 0.9], 3);
> g
g = 3
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
  • Ok. Thanks a lot. But can you please tell me if there's any library in python equivalent to matlab `dfilt.df2sos` ?? – MD. Khairul Basar Jul 13 '18 at 18:13
  • Isn't `g` supposed to be a vector ?? – MD. Khairul Basar Jul 13 '18 at 18:33
  • No, `g` is a scalar; it is the "overall gain". Note that if you use `sos = zp2sos(z, p, k)`, the gain is embedded in the first of the second order sections. See the [zp2sos documentation](https://www.mathworks.com/help/signal/ref/zp2sos.html) for more details. – Warren Weckesser Jul 13 '18 at 19:10
  • Ok. I thought it's a vector because in [`df2sos()`](https://in.mathworks.com/help/signal/ref/dfilt.df2sos.html) doc its stated that `g` is a gain vector. – MD. Khairul Basar Jul 13 '18 at 19:13