Matlab fft plot axes

3 views (last 30 days)
Iro Liontou
Iro Liontou on 11 Apr 2024
Answered: Star Strider on 11 Apr 2024
%
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?

Answers (1)

Star Strider
Star Strider on 11 Apr 2024
This would be my approach —
Fs = 1000;
Fn = Fs/2;
L = 60;
t = linspace(0, Fs*L, Fs*L+1).'/Fs; % Time Vector
EMG = sum(sin(2*pi*t*(1:50:450)),2); % Signal Vector
slen = size(EMG,1);
NFFT = 2^nextpow2(slen);
FTEMG = fft((EMG-mean(EMG)).*hann(slen), NFFT)/sum(hann(slen));
Fv = linspace(0, 1, NFFT/2+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
plot(Fv, abs(FTEMG(Iv))*2)
grid
xlabel('Frequency')
ylabel('Magnitude')
There are other ways to do this. This code is my usual approach.
.

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!