1

I need to design a smooting filter for computing the envelope of a signal. My specs in Matlab are

fs      = 44100;                    % Sampling frequency of 44100 Hz
fpass   = 2;                        % Passband edge frequency of 2 Hz
Wp      = fpass / (fs/2);           % Calculate normalized passband frequency
fstop   = 5;                        % Stopband edge frequency of 5 Hz
Ws      = fstop / (fs/2);           % Calculate normalized stopband frequency
Rp_lin  = 0.0001;                   % Max. passband ripple of 0.0001
Rp      = 20 * log10(1+Rp_lin);     % Calculate stopband ripple in dB
Rs      = 10;                       % Min. stopband attenuation of 10dB

%% Design the elliptic lowpass filter

% Calculate the order of the elliptic lowpass filter

[N, W] = ellipord(Wp, Ws, Rp, Rs);

% Calculate the coefficients of the elliptic lowpass filter via Bilinear Transformation

[b_ellip_lp, a_ellip_lp] = ellip(N, Rp, Rs, W, 'low');

% Convert the filter coefficients to second-order sections

[sos_ellip_lp, g_ellip_lp] = tf2sos(b_ellip_lp, a_ellip_lp);

%% Distribute the gain equally over the sections [N_sos, ~] = size(sos_ellip_lp);

sos_ellip_lp(:,1:3) = sos_ellip_lp(:,1:3) * (g_ellip_lp^(1/N_sos));

%% Normalize the coefficients

sos_ellip_lp = sos_ellip_lp / max(max(abs(sos_ellip_lp)));

%% Plot the 16-bit coefficients of the Elliptic low pass

% Quantize the coefficients for plotting

sos_ellip_lp_16bit = round(sos_ellip_lp * 2^(16-1));

figure(1) freqz(sos_ellip_lp_16bit); title('16-bit Elliptic low-pass filter');

I am designing an Elliptic filter with ellip. At first the gain is massive

enter image description here

After gain distribution over all biquads, it looks better.

enter image description here

After quantizing to 16 bit, I get a 10 dB gain!

enter image description here

I mean, I can just apply gain correction by trial and error. The filter is not adaptive and thus I can work with it. I would nevertheless like to not have these gain jumps. Filters for higher frequencies did not seem to have this issue.

neolith
  • 147
  • 5
  • Where are your poles and zeros before quantizing ? Where are they after quantizing? – Ben Mar 03 '21 at 18:36
  • Ah, the quantization error. Sure... We learned about this. I have indeed not checked and will do that first thing next morning. – neolith Mar 03 '21 at 18:41
  • @neolith If by "gain jumps" you mean over-/under-shoots, then elliptic is certainly not the way to go. If a smooth response is needed, try a Thiran, or Gaussian, but then your attenuation will suffer. But, if it's the envelope you're after, a Hilbert transformer might do wonders. – a concerned citizen Mar 03 '21 at 20:58
  • I will try another filter then. I was given the advice not to use the Hilbert transform by an experienced colleague, because it wouldn’t produce a usable envelope for a guitar signal. – neolith Mar 03 '21 at 21:04
  • @neolith Guitar is a different beast, true. Not only you'll need a large order, but because of the low frequencies you'll get only the envelope, but what you want (I presume) is a filtered version, a sort of a smoothed out peak detector. In that case, use a relatively low-Q filter, a compromise between overshoots and attenuation, since Thiran doesn't attenuate very well (but that's up to you to find out based on your data). BTW, use @ to notify users when you're replying to them (use <TAB> to cycle between names). – a concerned citizen Mar 04 '21 at 10:54
  • @aconcernedcitizen Why do I need a large order? I was trying to keep it as low as possible for processing speed. Yes, I want a (sort of) peak detector. The filtered square actually sounds better than the absolute. By calculating it this way I can also experiment with it a little. For a Thiran filter I would have to install the Communications toolbox. At least an Elliptic filter is possible. Chebyshev and Butterworth have a nominator of 0 after quantizing. – neolith Mar 04 '21 at 13:58
  • @neolith I was referring to the order of a HT: for 44.1 KHz and an assumed min. 100 Hz lowest freq. you'll need a large order. You don't need any toolbox, just use Thiran's paper. What did you mean by "nominator": numerator (N/d), or denominator (n/D)? Again, if you need as little overshoot as possible, you need low-Q filters. Anything higher than Bessel (Thiran) will give you overshoots. Gaussian is even better, but worse attenuation. If you don't have a choice, use Butterworth. – a concerned citizen Mar 04 '21 at 14:07
  • @aconcernedcitizen I meant numerator... I already tried butterworth. Doesn't seem to be possible. I need to filter out everything above 5-10 Hz actually because this is an automatically generated LFO for an effect. It is not easy it seems. – neolith Mar 04 '21 at 14:11
  • @neolith You're running into a different problem here: the poles are very close to the origin and there is a numeric limitation that you need to circumvent. There are answers to this, already, but I can't find them. – a concerned citizen Mar 04 '21 at 14:21
  • Yes, but I am running out of time. I think I will just stick to the gain correction for now. It doesn't sound bad. – neolith Mar 04 '21 at 14:39
  • Nah, this is never going to work with 16-bit coefficients. Guess I will have a look at the Bessel filter – neolith Mar 04 '21 at 16:05
  • @aconcernedcitizen I don't understand anything when reading this paper, but thank you for the hint. – neolith Mar 05 '21 at 07:54
  • @neolith This is the link. And for Thiran: $$H(z)=\left[\frac{\left( 2 N\right) \operatorname{!}}{N\operatorname{!}} \frac{1}{\prod_{i=N+1}^{2 N}{\left. 2 \tau+i\right.}} \frac{1}{\sum_{k=0}^{N}{\left. {{\left( -1\right) }^{k}} \begin{pmatrix}N\ k\end{pmatrix}, \prod_{i=0}^{N}{\left. \frac{2 \tau+i}{2 \tau+k+i}\right.} {{z}^{k}}\right.}}\right]$$ – a concerned citizen Mar 05 '21 at 08:39

0 Answers0