Borrar filtros
Borrar filtros

Help with matlab Butterworth filter with signal processing

16 visualizaciones (últimos 30 días)
Zainab
Zainab el 18 de Jul. de 2024 a las 17:23
Movida: Walter Roberson el 18 de Jul. de 2024 a las 20:01
Hello everyone,
I am currently using Matlab 2020 to run semg signals the matlab's simulink to process the signal. I am on the last step and need to add a Butterworth filter I am running into error messages that I do not know how to fix. This is for live signal processing aswell. I will attach the code below and the error messages that I am recieving I would highly apperciate it if someone could fix the code and explain what they did. I am still learning how to code in Matlab. Thank you in advance
function y = fcn(u, fs)
persistent Hd;
persistent filter_initialized;
if isempty(filter_initialized)
filter_order = 5;
low_cutoff = 20;
high_cutoff = 500;
Wn = [low_cutoff high_cutoff] / (fs / 2);
[b, a] = butter(filter_order, Wn, 'bandpass');
[sos,g] = butter2sos(b,a);
g = sqrt(prod(Wn));
Hd = dsp.BiquadFilter('SOSMatrix', sos, 'ScaleValues', g);
filter_initalized = true;
end
y = step(Hd, u);
end
function [sos, g] = butter2sos(b,a)
end
errors that I am getting below:
Output argument 'sos' is not assigned on some execution paths. Function 'MATLAB Function1' (#36.473.476), line 25, column 11: "sos" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Function call failed. Function 'MATLAB Function1' (#36.284.309), line 17, column 1: "[sos,g] = butter2sos(b,a)" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'sos'. The first assignment to a local variable determines its class. Function 'MATLAB Function1' (#36.285.288), line 17, column 2: "sos" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'g'. The first assignment to a local variable determines its class. Function 'MATLAB Function1' (#36.289.290), line 17, column 6: "g" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'sos'. The first assignment to a local variable determines its class. Function 'MATLAB Function1' (#36.372.375), line 20, column 36: "sos" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Persistent variable 'Hd' must be assigned before it is used. The only exception is a check using 'isempty(Hd)' that can be performed prior to assignment. Function 'MATLAB Function1' (#36.450.452), line 23, column 12: "Hd" Launch diagnostic report.

Respuesta aceptada

Umar
Umar el 18 de Jul. de 2024 a las 19:03
Movida: Walter Roberson el 18 de Jul. de 2024 a las 20:01
Hi Zainab,
After analyzing your code, there were several bugs found in the code as mentioned below.
The variable filter_initialized is misspelled as filter_initalized.
The function butter2sos is called but not implemented in the provided code.
The variables sos and g are not assigned values in all execution paths, leading to errors.
The Hd variable is used before being assigned in all code paths.
Here is the corrected code with the necessary fixes:
function y = fcn(u, fs)
persistent Hd;
persistent filter_initialized;
if isempty(filter_initialized)
filter_order = 5;
low_cutoff = 20;
high_cutoff = 500;
Wn = [low_cutoff high_cutoff] / (fs / 2);
[b, a] = butter(filter_order, Wn, 'bandpass');
[sos, g] = butter2sos(b, a);
g = sqrt(prod(Wn));
Hd = dsp.BiquadFilter('SOSMatrix', sos, 'ScaleValues', g);
filter_initialized = true;
end
y = step(Hd, u);
end
function [sos, g] = butter2sos(b, a)
% Implement the conversion of Butterworth filter coefficients to SOS
% and scale values here
% Example implementation:
[sos, g] = tf2sos(b, a);
end
By implementing the butter2sos function and correcting variable assignments and initialization, the errors related to undefined variables and missing assignments should be resolved. Remember to adjust the butter2sos function implementation based on the desired conversion method for Butterworth filter coefficients to second-order sections. Hope, you can share the code with your team to help you with implementation of butter2sos function or you can try yourself to learn more about coding in Matlab. The more you learn, the better you will become at it and learning comes from learning mistakes and more experiments with coding. Good luck!

Más respuestas (0)

Categorías

Más información sobre Programmatic Model Editing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by