# Plot mean frequency of EMG signals

45 views (last 30 days)
Prawee Pongsing on 3 Jan 2023
Commented: William Rose on 4 Jan 2023
Hello everyone, I'm a newbie in matlab coding. Now I have an EMG signals and I want to plot it to see the change in mean frequency like the one in this paper which I have tried to write as below:
% Load the data from the CSV file
data = readmatrix('data_emg_test.csv');
% Extract the time and EMG data columns
time = data(:,1);
emg = data(:,2);
% Compute the Fourier transform of the EMG signal
fourier_transform = fft(emg);
% Compute the power spectrum of the EMG signal
power_spectrum = abs(fourier_transform).^2;
% Compute the frequency axis of the power spectrum
Fs = 1000; % Sampling frequency
N = length(emg); % Number of time points in the signal
duration = time(end) - time(1); % Duration of the signal
frequency_axis = (0:N-1)/N*Fs/duration;
% Compute the mean frequency of the EMG signal
mean_frequency = meanfreq(power_spectrum,frequency_axis);
% Plot the mean frequency over time
plot(time, mean_frequency)
% Add labels to the x- and y-axis
xlabel('Time (s)')
ylabel('Mean Frequency (Hz)')
% Add a title to the plot
title('Mean Frequency of EMG Signal')
% Display grid lines on the plot
grid on
% Set the limits of the x- and y-axis
xlim([0 time(end)])
ylim([0 max(mean_frequency)])
But I'm very inexperienced in using matlab so the results are not as good as I hoped. And I don't know how to fix it or what examples to refer to from each thread, please help me.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

William Rose on 3 Jan 2023
Alty & Georgakis (2011), whom you cite, use their eqn.7 to compute the mean frequency.
You do not have to compute the FFT or power spectrum, since meanfreq() does that for you. You do need to extract segments of the signal. In the attached code, I use a segment duration of 0.25 s, to match the duration used by Alty & Georgakis (2011). I choose to overlap the segments by half a segment. This is a common choice. I calculate the total number of segments (Nseg) that will fit into the full recording. I also compute the midpoint time of each segment.
After doing the preparatory steps above, I do the main loop. The loop extracts each segment from the full-length record, and computes the mean frequency for that segment.
The last step is to plot the results, as shown below, using the attached script and your data.
##### 2 CommentsShowHide 1 older comment
William Rose on 4 Jan 2023
Your written communication in English is perfect! You are certainly not weak in language!
You're welcome.

Sign in to comment.

### Categories

Find more on Spectral Measurements in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by