Clear Filters
Clear Filters

How can I apply a lowpass filter samplewise in my code?

33 views (last 30 days)
I have a real time plugin that does a little bit of reverberation. After each delay line in v(n) I want to apply a lowpass filter to cut out the high frequencies. How can I do that?
My code below:
function out = process(plugin, in)
out = zeros(size(in));
for i = 1:size(in,1)
% Summieren der L/R - Kan�le
inL = in(i,1);
inR = in(i,2);
inSum = (inL + inR)/2;
plugin.buffInput(plugin.pBuffInput + 1) = inSum;
% loop over delay lines
for n=1:plugin.N
% d_n = gain * delayed v_n
for k=1:plugin.N
plugin.d(k) = plugin.g(k) * plugin.buffDelayLines(k, mod(plugin.pBuffDelayLines + plugin.m(k), plugin.maxDelay +1) + 1);
end
% f_n = A(n,:) * d'
plugin.f(n) = plugin.A(n,:) * plugin.d(:);
% v_n with pre delay
plugin.v(n) = plugin.b(n) * plugin.buffInput(mod(plugin.pBuffInput + plugin.preDelayS, (plugin.maxPreDelay * plugin.fs + 1)) + 1) ...
+ plugin.f(n);
plugin.buffDelayLines(n, plugin.pBuffDelayLines + 1) = plugin.v(n);
% output lines
plugin.s(n) = plugin.c(n) * plugin.d(n);
out(i,:) = out(i,:) + real(plugin.s(n));
end
% Assign to output
out(i,1) = plugin.mix/100 * out(i,1) + (1.0 - plugin.mix/100) * in(i,1);
out(i,2) = plugin.mix/100 * out(i,2) + (1.0 - plugin.mix/100) * in(i,2);
calculatePointer(plugin);
end
end

Answers (2)

Drishti
Drishti on 6 Sep 2024 at 12:24
Hi Muhsin,
I understand that you are trying to implement a low pass filter to cut out the high frequencies.
To include the low-pass filter, refer to the implemented code:
% v_n with pre delay
rawVn = plugin.b(n) * plugin.buffInput(mod(plugin.pBuffInput + plugin.preDelayS, (plugin.maxPreDelay * plugin.fs + 1)) + 1) ...
+ plugin.f(n);
% Apply low-pass filter
plugin.v(n) = alpha * rawVn + (1 - alpha) * prevY(n);
prevY(n) = plugin.v(n);
plugin.buffDelayLines(n, plugin.pBuffDelayLines + 1) = plugin.v(n);
To achieve this, I have made certain assumptions which includes ‘cuttoffFreq’ and ‘alpha’ parameters as mentioned below:
% Define the cutoff frequency and calculate alpha
cutoffFreq = 100; % Example cutoff frequency in Hz
alpha = (2 * pi * cutoffFreq) / (plugin.fs + 2 * pi * cutoffFreq);
% Initialize the previous output for the filter
prevY = zeros(plugin.N, 1);
I hope this helps in applying the low pass filter in the provided code.
  1 Comment
Muhsin Zerey
Muhsin Zerey on 7 Sep 2024 at 9:51
Edited: Muhsin Zerey on 7 Sep 2024 at 10:06
Hi Thanks very much for your help! Do you know how I can apply a butterworth first order as a lowpass filter?

Sign in to comment.


Image Analyst
Image Analyst on 8 Sep 2024 at 3:36
One way is movmean.
  3 Comments
Image Analyst
Image Analyst on 8 Sep 2024 at 18:32
You just pass your signal to it. The wider the window, the more samples are included in your average and the smoother the signal will be. Smoothing a signal (replacing elements by the local average) is a low pass filter operation. It's the same thing as convolution or Fourier filtering.
Muhsin Zerey
Muhsin Zerey on 9 Sep 2024 at 7:31
How can I apply it to my code tho? I will need the previous and future next sample to maake an average.

Sign in to comment.

Categories

Find more on Audio Processing Algorithm Design in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!