Signal analysis... can't figure out how finish the program... Please help ! 2 parameters left and one small thing.

3 views (last 30 days)
Hello everyone,
I'll start by discribing what i try to achieve and then i show you the code.
I'm doing a small program in order to analyse an EMG dataset from a .mat, i'd like to create plots with relevant information, i did some you will see in the code, and extract data to write them in an xls, and if it's possible to saveas something in order to reuse each data extracted from the analysis separatly .
I have 4 muscles to analyse when they realyse 6 tasks.
I want to extract 3 parameters from that, and write them in seperate sheets in an xls:
  • The Frequency Peak of each muscle associate with the task
  • The rhythmicity index IR = (Pic_Power / Total_Power) * 100 with Pic_Power defined as being the peak alone without its harmonics, or said like that IR = Area under peak / Total area
  • The dispersion index ID = (delta(Fpow68) * 100) / (delta)Fetude
I think i managed to find the Frequency Peak, but i have difficulties to write them down in an xls sheet in each cell, the loops i made just eraze everything and i have only the last one, but the 2 last, IR and ID, i just don't know how to find them and plot them.
Un commentary there is are my question well marked don't worry, i'm not planing to make you loose time :)
Hopping my code is readable for everyone, but if you have some advice in optimization after fixing my questions, i'm open to everything :)
If you're still there, THANKS !
Now i show you the code, first the main and then the functions:
Main:
%load data from dataset
close all;
clearvars;
load Dataset\posture.mat;
signal=datablock1.data;
%Variables
Fe=1000;
t=0:0.001:10;
Ordre_BP= 2;
BP_Fc=[20,450];
LP_Fc=30;
Ordre_LP=2;
Nb_Pt=10001;
f_DSP=0:0.1:500;
%Patient's data
Tremor_functions.data_patient;
%Loop fonctions for signal analysis and plot
for mrkr=1:6
Tremor_functions.Timestamp(markers,mrkr,Fe)
for muscle=1:4
Tremor_functions.signal_processing(Debut,Fin,muscle,Nb_Pt,signal,Fe,mrkr,t,Ordre_BP,BP_Fc,LP_Fc,Ordre_LP)
Tremor_functions.subplot(mrkr,muscle,t,F,f_DSP,D,folder_patient)
end
end
%Write XLS
Tremor_functions.remplir_excel(folder_patient,E,A,V,M,T,T_c,Pik)
Now my functions:
classdef Tremor_functions
methods (Static)
function data_patient
%Patient's identity and evaluation date
Prompt={'Nom Majuscule', 'Prenom', 'Date naissance (jj mm aaaa)','Date Evaluation (aaaa mm jj)'};
Dlgtitle='Donnees patient';
Def={'Marcel','Petit','01 01 1961','2015 02 16'};
Answer=inputdlg(Prompt,Dlgtitle,1,Def);
Info_patient={Answer{1},Answer{2},Answer{3}};
%Assignation info_patient in cell aray A
a = [{Answer{1}},{Answer{2}},{Answer{3}}];
assignin('base','A',a);
Eval_date={Answer{4}};
%Assignation Eval date for dir name patient in char vector
e = [Answer{4}];
assignin('base','E',e);
%Assignation Eval date for xls writting
v = [{Answer{4}}];
assignin('base','V',v);
Muscle={'Fl_Pr','Ex_Su','Bic','Tri'};
%Assignation Muscle for xls writting
assignin('base','M',Muscle);
%Assignation Taches for xls writting
Taches=[1;2;3;4;5;6];
assignin('base','T',Taches);
%Assignation of word Tâches for xls writting
Taches_c={'Taches'};
assignin('base','T_c',Taches_c);
%Assignation dir for patient's folder in char vector
folder_patient = [Answer{1},' ',Answer{2},' ',num2str(Answer{3})];
assignin('base','folder_patient',folder_patient)
%Creation if folder_patient's folder doesn't exist
if ~exist(folder_patient, 'dir')
mkdir(folder_patient);
end
%PLEASE HELP : Assignation subfolder dir in char vector - NOT WORKING PLEASE HELP
%I'm trying to create a sub folder in the patient_folder in order to stock the XLS, plots and possibly other data
folder_evalplot = [folder_patient\(Answer{4})];
assignin('base','folder_evalplot',folder_evalplot)
%Creation of the subfolder evalplot if it doesn't exist
if ~exist(folder_evalplot, 'dir')
mkdir(folder_evalplot);
end
%xls name depends of info patient
filename = 'Info_patient.xlsx';
assignin('base','filename',filename)
%Association folder, fichier
fullname = fullfile(folder_patient, filename);
assignin('base','fullname',fullname)
end
function Timestamp(markers,mrkr,Fe)
%Assgination when the first task begins
Deb=markers(mrkr)*Fe;
start=Deb;
assignin('base','Debut',start);
%Assignation when the task is ending, every 10 seconds
End=(markers(mrkr)+10)*Fe;
ending=End;
assignin('base','Fin',ending);
end
function signal_processing(Debut,Fin,muscle,Nb_Pt,signal,Fe,mrkr,t,Ordre_BP,BP_Fc,LP_Fc,Ordre_LP)
EMG_Brut=signal(Debut:Fin,muscle);
%HELP HERE TOO
%HERE I HAVE SOME QUESTION... SHOULD I BEGIN WITH THE FFT, then Energy spectral density, then Power spectranl density...
%AND AFTER THAT APPLY THE FILTERS ? OR DO THE OPPOSITE ? FIRST THE FILTERS AND THEN LE FFT DSE AND DSP ? Or maybe it's another sequence.
%SHOULD I PASS THOSE AFTER THE FILTERS ?
% % Transformée de Fourrier (FFT)
% Signal_FFT=fft(EMG_Brut,Nb_Pt)/Nb_Pt;
%
% % ENERGY SPECTRAL DENSITY (DSE)
% %f=-500:0.1:500;
% Signal_DSE=Signal_FFT.*conj(Signal_FFT);
% Signal_DSE=fftshift(Signal_DSE);
%
% % POWSER SPECTRAL DENSITY (DSP)
% %f_DSP=0:0.1:500;
% Signal_DSP=Signal_DSE*(Fe/Nb_Pt);
% Nb_Pt_F=fix(Nb_Pt/2);
% Signal_DSP=Signal_DSP(Nb_Pt_F+1:Nb_Pt);
%
% %Assignation de signal_DSP
% d = [Signal_DSP];
% assignin('base','D',d)
% Filtre Butterworth passe bas
[b,a]=butter(Ordre_BP,BP_Fc/(Fe/2),'bandpass');
EMG_filt=filtfilt(b,a,EMG_Brut);
% Rectification
EMG_filt=abs(EMG_filt);
% Filtrage Butterworth passe bas
[b,a]=butter(Ordre_LP,LP_Fc/(Fe/2),'low');
EMG_filt=filtfilt(b,a,EMG_filt);
% Supression de la composante continue :
mean_filt=mean(EMG_filt);
EMG_filt=EMG_filt - mean_filt;
EMG_filt=detrend(EMG_filt,'constant');
%Assignation d'EMG_filt
filt= [EMG_filt];
assignin('base','F',filt)
% Transformée de Fourrier (FFT)
Signal_FFT=fft(EMG_Brut,Nb_Pt)/Nb_Pt;
% Densité spectrale d'Energie (DSE)
%f=-500:0.1:500;
Signal_DSE=Signal_FFT.*conj(Signal_FFT);
Signal_DSE=fftshift(Signal_DSE);
% Densité spectrale de puissance (DSP)
%f_DSP=0:0.1:500;
Signal_DSP=Signal_DSE*(Fe/Nb_Pt);
Nb_Pt_F=fix(Nb_Pt/2);
Signal_DSP=Signal_DSP(Nb_Pt_F+1:Nb_Pt);
%Assignation de signal_DSP
d = [Signal_DSP];
assignin('base','D',d)
% HERE NEED HELP !!!
%Indices Calculations
%Parameters
fmin=1.5;
fmax=12;
i_min=fmin*10;
i_max=fmax*10;
%Fpeak(max) calculation
DSP=Signal_DSP(i_min:i_max);
[DSPmax(mrkr,muscle),ipic]=max(DSP);
Fpic(mrkr,muscle)=(ipic/10)+1.5;
Pic= [Fpic];
assignin('base','Pik',Pic)
%HELP IR calculation .... Mystery
IR=(FPic/DSP)*100 % It's the idea but i don't know how to write it with matlab :/
%HELP !!! Dispersion Indice
ID = (delta(Fpow68) * 100) / (delta)Fetude % Also, that's the idea but mystery :(
end
function subplot(mrkr,muscle,t,F,f_DSP,D,folder_patient) % Working for ploting, i'd like to add more details but i
%Want to save them in the specific subolder evoked above.
cellmuscle = ["Fl-Pr","Ex-Su","Bic","Tri"];
task = mrkr*ones(1,4);
nombre = [cellmuscle(muscle) '/Tache' num2str(task(muscle))];
Muscle_Tache=strcat('Tache',num2str(mrkr),'Muscle',num2str(muscle));
Muscle_Tache=figure('Name',Muscle_Tache,'Position',[0 0 600 600]);
movegui(Muscle_Tache,'northwest');
subplot(2,1,1)
plot(t,F,'r')
title(nombre);
xlabel('Temps (s)')
ylabel('EMG (mV)')
subplot(2,1,2)
plot(f_DSP,D,'b')
xlim([0 400])
title(nombre);
xlabel('Fréquence (Hz)')
ylabel('DSP (mv2/Hz)')
Muscle_Task=strcat('Tache',num2str(mrkr),'Muscle',num2str(muscle),'.bmp');
saveas(gcf,fullfile(folder_patient, Muscle_Task),'bmp'); % THIS LINE DONT WORK
%saveas(Muscle_Tache,Muscle_Task); this line is working but not doing what i need to
end
function remplir_excel(folder_patient,E,A,V,M,T,T_c,Pik) % Well, with this function, the "Pik" FPIC B5 does write something,
% but only the last loop in the last cell, and everything else are 0. Also, i need to write the others indices but that...
%I know how to, but i'd like to save it in the subfolder evoked above...
xlswrite(fullfile(folder_patient,E),A,'Fpic','A2');
xlswrite(fullfile(folder_patient,E),V,'Fpic','A3');
xlswrite(fullfile(folder_patient,E),M,'Fpic','B4');
xlswrite(fullfile(folder_patient,E),T,'Fpic','A5');
xlswrite(fullfile(folder_patient,E),T_c,'Fpic','A4');
xlswrite(fullfile(folder_patient,E),Pik,'Fpic','B5');
end
end
end
If you're still here, an amazing thank you for your help... i'm new on matlab, sorry if it's obvious for you, my head is going to explode right now. I tried everything i could till now.
Thanks again everyone ! Hopping you're well.

Answers (1)

Matt Gaidica
Matt Gaidica on 13 Dec 2020
Edited: Matt Gaidica on 13 Dec 2020
What version of MATLAB are you using? See that xlswrite() is not recommended: https://www.mathworks.com/help/matlab/ref/xlswrite.html
In MATLAB 2020, write functions (writematrix(), writetable()) have the ability to append data. I don't quite understand all that you're doing, but you would either need to add your data to a matrix/table and kickoff a write-to-xls at the end, or use the append option.

Community Treasure Hunt

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

Start Hunting!