MATLAB is a powerful tool for generating synthetic motion data for machine learning applications. With built-in functions such as the Signal Processing Toolbox and Image Processing Toolbox and capabilities, MATLAB makes it easy to simulate real-world sensor data, generate labelled datasets, and preprocess data for edge AI applications.In this tutorial, you will learn how to:
Example: Generating continuous motion data (up-down snake wave and idle) in MATLAB
Below, we recreate a continuous motion sample project that could be used to test wearable sensors, monitor vibrations, or simulate small repetitive movements in an industrial setting.
You can run MATLAB entirely in the browser using MATLAB Online. This makes it easy to share your project, collaborate, or quickly try out scripts without installing anything locally.
Running the Script: Generates labeled time-series data (e.g., idle, snake, updown, wave).
Save to CSV: The script automatically writes to motion_data.csv.Visualize: MATLAB’s plot and subplot functions help verify that the signals make sense.Below is a minimal code snippet compare with your own script for advanced features:
If you want to generate multiple classes like idle, snake, updown, wave you can segment your time vector and assign labels programmatically.Here’s a minimal example:
Copy
% Clear workspaceclear; clc; rng(0); % set random seed% Sampling settingsfs = 62.5;t_end = 60; % 1 minute total for examplet = 0:1/fs:t_end-1/fs;% Pre-allocate signal arraysmotion_signal = zeros(size(t));labels = strings(size(t)); % label each sample% Example: define time ranges for each motionidle_duration = 10; % first 10s idlesnake_duration = 20; % next 20s snakeupdown_duration = 30; % last 30s up-down% Generate Idleidle_idx = t <= idle_duration;motion_signal(idle_idx) = 0 + 0.05*randn(1, sum(idle_idx)); labels(idle_idx) = "idle";% Generate Snake motionsnake_idx = t > idle_duration & t <= (idle_duration + snake_duration);snake_t = t(snake_idx) - idle_duration;motion_signal(snake_idx) = 0.3 * sin(2*pi*0.8 * snake_t) + 0.05*randn(size(snake_t));labels(snake_idx) = "snake";% Generate Up-Down motionupdown_idx = t > (idle_duration + snake_duration);updown_t = t(updown_idx) - (idle_duration + snake_duration);motion_signal(updown_idx) = 0.8 * sin(2*pi*0.5 * updown_t) + 0.1*randn(size(updown_t));labels(updown_idx) = "updown";% Combine data and labels into one matrixcombined_data = [t' motion_signal'];csv_filename = 'motion_data.csv';% Write numeric datawritematrix(combined_data, csv_filename);% Write labels in a second file or append to CSV with e.g. "writetable" T = table(t', motion_signal', labels', 'VariableNames', {'time','motion','label'});writetable(T, 'motion_data_labeled.csv');disp(['Data saved as ', csv_filename, ' and motion_data_labeled.csv']);% Quick plotfigure;plot(t, motion_signal, 'LineWidth', 1.2);xlabel('Time (s)');ylabel('Amplitude');title('Synthetic Multi-Class Motion');
In practice, you can repeat this process for each axis (e.g., x, y, z) and store all signals plus labels in a single table.
Once you have your synthetic data, you can use it to train a model in Edge Impulse. Check out the Continuous motion recognition project for a complete example.
For a more advanced example, see the public project:Rolling Element Bearing Fault Diagnosis that uses MATLAB to generate synthetic vibration data for bearing fault detection, based on the MATLAB Rolling Element Bearing Fault Diagnosis example.
By leveraging MATLAB for synthetic data generation, you can rapidly prototype and iterate without the overhead of physical sensors or mechanical rigs. This approach helps fill in dataset gaps, improves model robustness, and speeds up development cycles. Please share your own experience with MATLAB and other uses or projects with us on our forum.
Integrate Custom MATLAB DSP blocks in Edge Impulse for advanced preprocessing before training your models. Check out the MATLAB DSP custom processing block.