clc;
clear;
close all;
%% Visualization loop for raw EMG signal
% Parameters
sampling_frequency = 1000; % Sampling frequency in Hz
duration = 5; % Duration of signal acquisition in seconds
num_samples = sampling_frequency * duration;
% Initialize variables
time = (0:num_samples-1)/sampling_frequency;
frequency = linspace(0, sampling_frequency/2, num_samples/2);
x = zeros(1, num_samples);
a = arduino();
%figure('Name', 'Signal from EMG sensor');
%grid on;
%title('Signal from EMG sensor');
%xlabel('Time (s)');
%ylabel('Voltage');
% Plot frequency spectrum
figure('Name', 'Frequency Spectrum');
grid on;
title('Frequency Spectrum of EMG Signal');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% Start loop to acquire signal
for i = 1:num_samples
% Read voltage
voltage = readVoltage(a, 'A0');
% Store voltage
x(i) = voltage;
% Update plot
plot(time(1:i), x(1:i));
xlim([0, duration]); % Adjust xlim as needed
drawnow;
end
% Calculate Fourier Transform
X = fft(x);
X_magnitude = 2*abs(X(1:num_samples/2))/num_samples;
% Plot frequency spectrum
figure('Name', 'Frequency Spectrum');
plot(frequency, X_magnitude);
title('Frequency Spectrum of EMG Signal');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
xlim([0, max(frequency)]); % Adjust xlim as needed
I have this code in MATLAB. I want to acquire EMG signal from Arduino and i tried FFT, but i want the y axis to have magnitude values and the x axis to have the frequency values. how do I do that?