How can I convert codes from MATLAB to Arduino
Show older comments
Hello, I'm newly in MATLAB and Arduino. I have known that MATLAB could convert codes from m files to C code. So I think it possible to get C code and then compile to Arduino hardware. Please give me some direction, How can I do that?
Thanks, chirawat
1 Comment
dawit abadi
on 9 Jun 2018
i also have arduino code and i want to convert it to matlab code ,how can i do it
Answers (2)
Jan
on 15 Nov 2012
1 vote
This is not trivial and it depends on the code itself. E.g. calls of input or ode45 will not run on the Arduino. Could you post the code, which you want to convert?
4 Comments
Vaidyanathan Narayanan
on 26 Jan 2017
Edited: Walter Roberson
on 26 Jan 2017
Please can you help me in converting this MATLAB Code to Arduino ??
MATLAB Code For PAM (Pulse-amplitude modulation)
clc;
close all;
clear all;
a = input('Enter the amplitude = ');
f = input('Enter the frequency = ');
t = 0:0.02:2; % for a total of 16 samples
x1 = 1; %generation of an impulse signal
x2 = a*sin(2*pi*f*t); %generation of sine wave
y = x1.*x2; %modulation step
subplot(3,1,1); %for impulse signal plot
stem(x1);
title('Impulse Signal');
xlabel('Time');
ylabel('Amplitude ');
subplot(3,1,2) %for sine wave plot
plot(t,x2);
title('Sine Wave');
xlabel('Time ');
ylabel('Amplitude ');
subplot(3,1,3) %for PAM wave plot
stem(t,y);
title('PAM Wave');
xlabel('Time');
ylabel('Amplitude');
Walter Roberson
on 26 Jan 2017
That code cannot be converted to Arduino, as Arduino does not support plotting. You would also need to define where the input are to come from.
Mirza Riyasat Ali
on 30 Jul 2020
Edited: Walter Roberson
on 6 Aug 2020
can you help me converting this code to arduino
% Matlab Program to demonstrate the concpet of "Signal Smoothing"
% Signal Smoothing or Averaging is the Fundamental Noise Reduction Tool in
% 1-D Signal Processing Such as a Monotonic Signal, Speech or Voice.
clc;
clear all;
close all;
% Initialise the Time
t = 0:0.001:1; % 0 -- Starting Time, 0.001-- Sampling Time, 1 -- Ending Time
% reading the size of the time
[m,n] = size(t);
% Generation of sine wave
% amplitude
a = 2;
% frequency
f = 4;
% sine wave argument according to the definition
y = a*sin(2*pi*f*t);
% Generation of Random signal using "rand" command in the matlab
% Please see the sintax of "rand" command by typing help rand in the matlab
% command window
r = rand(m,n);
% Note: Observer that, we generated the random signal in the matlab using
% rand function such that, size of the signal and random signal both are
% similar.
% Adding both signal and noise
y1 = (y + r);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%Implementation of Moving Average Filter or Smoothing Filter%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Algorithm:
% X(n-1) + X(n) + X(n+1)
% Y(n) = ----------------------
% 3
%
% here, X(n) = noise effected signal
% Y(n) = Smoothed or averaged signal
% therefore,
% Let's Initiliaze the for loop from 2:n-1 in order to validate the real
% meaning of (n-1) and (n+1)
% let the ouput smoothed signal is y2(n)
% Let's create an empty array y2(n), size equal to the size of the input
% signal
y2 = zeros(m,n);
for i = 2:(n-1)
y2(i) = (y1(i-1) + y1(i) + y1(i+1))/3;
end
% therefore y2(i) will be the smoothed signal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Lets plot the signals and see the output
subplot(411);
plot(t,y); title('input signal');
subplot(412);
plot(t,r); title('random signal');
subplot(413);
plot(t,y1); title('noise added signal');
subplot(414);
plot(t,y2); title('smoothed signal');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Please try this implementation an extension
% initiate a for loop for 2-4 times, and make y2 = y1, i.e., we are making
% a feed back, it becomes a recursive filter, this will be the first step
% towards the implementation of Recursive filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% For any questions please send a mail to samudrala.naren@gmail.com
% Implemented by : Jagadeesh Samudrala, Asst. Prof., Dept. of ECE, Aditya
% Engineering College.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Walter Roberson
on 6 Aug 2020
Arduino does not support plotting, so that code cannot be converted.
The numeric part up to the calculation of y2 could be generated for Arduino, if you had the MATLAB Coder toolbox, or if you are willing to embed the code within a Simulink project (Simulink can generate code for Arduino even without MATLAB Coder, but you would have to wrap some of the functionality within a MATLAB Function Block.)
chirawat
on 15 Nov 2012
0 votes
Categories
Find more on MATLAB Support Package for Arduino Hardware 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!