Converting MATLAB code to Arduino code
Show older comments
I need to convert this code made in MATLAB for use using the Arduino program which I believe uses C code. I am not sure how to do so and any help will be apreciated! It is for filtering data a data set.
function Hd = Lowpass_2
%LOWPASS_2 Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.5 and Signal Processing Toolbox 8.1.
% Generated on: 08-Mar-2019 11:28:05
% Equiripple Lowpass filter designed using the FIRPM function.
% All frequency values are in Hz.
Fs = 100; % Sampling Frequency
Fpass = 0.4; % Passband Frequency
Fstop = 0.45; % Stopband Frequency
Dpass = 0.057501127785; % Passband Ripple
Dstop = 0.0001; % Stopband Attenuation
dens = 20; % Density Factor
% Calculate the order from the parameters using FIRPMORD.
[N, Fo, Ao, W] = firpmord([Fpass, Fstop]/(Fs/2), [1 0], [Dpass, Dstop]);
% Calculate the coefficients using the FIRPM function.
b = firpm(N, Fo, Ao, W, {dens});
Hd = dfilt.dffir(b);
% [EOF]
1 Comment
madhan ravi
on 27 Mar 2019
Have a look into Embedded coder and MATLAB coder.
Answers (3)
Mark Sherstan
on 27 Mar 2019
0 votes
The easiest way I find is creating a Simulink model and then deploying it on the hardware. These links should get you started:
- https://www.mathworks.com/hardware-support/arduino-simulink.html
- https://www.mathworks.com/help/supportpkg/arduino/ug/run-model-on-arduino-hardware.html
- https://www.mathworks.com/help/supportpkg/arduino/examples/getting-started-with-arduino-hardware.html
Fakhril
on 24 Jun 2026 at 13:26
Edited: Walter Roberson
on 24 Jun 2026 at 15:46
[System]
Name='Simulasi_TDS_2'
Type='mamdani'
Version=2.0
NumInputs=1
NumOutputs=2
NumRules=0
AndMethod='min'
OrMethod='max'
ImpMethod='min'
AggMethod='max'
DefuzzMethod='centroid'
[Input1]
Name='TDS'
Range=[0 1000]
NumMFs=3
MF1='Rendah':'trapmf',[0 0 150 220]
MF2='Normal':'trapmf',[180 200 600 620]
MF3='Tinggi':'trapmf',[580 650 1000 1000]
[Output1]
Name='Pompa_1'
Range=[0 400]
NumMFs=4
MF1='Sedikit':'trimf',[30 100 170]
MF2='Banyak':'trapmf',[230 310 400 400]
MF3='Tidak_Perlu':'trapmf',[0 0 30 60]
MF4='Sedang':'trimf',[120 200 280]
[Output2]
Name='Pompa_2'
Range=[0 400]
NumMFs=4
MF1='Tidak_Perlu':'trapmf',[0 0 30 60]
MF2='Sedang':'trimf',[120 200 280]
MF3='Banyak':'trapmf',[230 310 400 400]
MF4='Sedikit':'trimf',[30 100 170]
[Rules]
1 Comment
Walter Roberson
on 24 Jun 2026 at 15:48
This appears to be a ruleset for fuzzy logic. It is completely unclear to me how this answers the question of how to convert MATLAB code into Arduino code.
Fakhril
on 24 Jun 2026 at 13:30
Edited: Walter Roberson
on 24 Jun 2026 at 15:46
[System]
Name='Simulasi_TDS_2'
Type='mamdani'
Version=2.0
NumInputs=1
NumOutputs=2
NumRules=0
AndMethod='min'
OrMethod='max'
ImpMethod='min'
AggMethod='max'
DefuzzMethod='centroid'
[Input1]
Name='TDS'
Range=[0 1000]
NumMFs=3
MF1='Rendah':'trapmf',[0 0 150 220]
MF2='Normal':'trapmf',[180 200 600 620]
MF3='Tinggi':'trapmf',[580 650 1000 1000]
[Output1]
Name='Pompa_1'
Range=[0 400]
NumMFs=4
MF1='Sedikit':'trimf',[30 100 170]
MF2='Banyak':'trapmf',[230 310 400 400]
MF3='Tidak_Perlu':'trapmf',[0 0 30 60]
MF4='Sedang':'trimf',[120 200 280]
[Output2]
Name='Pompa_2'
Range=[0 400]
NumMFs=4
MF1='Tidak_Perlu':'trapmf',[0 0 30 60]
MF2='Sedang':'trimf',[120 200 280]
MF3='Banyak':'trapmf',[230 310 400 400]
MF4='Sedikit':'trimf',[30 100 170]
[Rules]
1 Comment
Walter Roberson
on 24 Jun 2026 at 15:47
What is the difference between this answer and your previous answer https://www.mathworks.com/matlabcentral/answers/452684-converting-matlab-code-to-arduino-code#answer_1575635 ?
Categories
Find more on Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!