fs = 3000;
localfile = matlab.internal.examples.downloadSupportFile("SPT","data/MyoelectricData.zip");
datasetFolder = fullfile(fileparts(localfile),"MyoelectricData");
if ~exist(datasetFolder,"dir")
unzip(localfile,datasetFolder)
end
sds1 = signalDatastore(datasetFolder,IncludeSubFolders=true,SampleRate=fs);
p = endsWith(sds1.Files,"d.mat");
sdssig = subset(sds1,p);
sds2 = signalDatastore(datasetFolder,SignalVariableNames=["motion";"data_indx"],IncludeSubfolders=true);
p = endsWith(sds2.Files,"i.mat");
sdslbl = subset(sds2,p);
signal = preview(sdssig);
for i = 1:8
x(i) = subplot(4,2,i);
plot(signal(:,i));
title("Channel"+i);
end
linkaxes(x,"y");
lbls = {};
i = 1;
while hasdata(sdslbl)
label = read(sdslbl);
idx_start = label{2}(2:end-1)';
idx_end = [idx_start(2:end)-1;idx_start(end)+(3*fs)];
val = categorical(label{1}(2:end-1)',[1 2 3 4 5 6 7], ...
["HandOpen" "HandClose" "WristFlexion" "WristExtension" "Supination" "Pronation" "Rest"]);
ROI = [idx_start idx_end];
% In some cases, the number of label values and ROIs are not equal.
% To eliminate these inconsistencies, remove the extra label value or ROI limits.
if numel(val) < size(ROI,1)
ROI(end,:) = [];
elseif numel(val) > size(ROI,1)
val(end) = [];
end
lbltable = table(ROI,val);
lbls{i} = {lbltable};
i = i+1;
end
lblDS = signalDatastore(lbls);
lblstable = preview(lblDS);
lblstable {1}
DS = combine(sdssig,lblDS);
combinedData = preview(DS);
figure;
msk = signalMask(combinedData{2});
plotsigroi(msk,combinedData{1}(:,1));
tDS = transform(DS,@preprocess);
transformedData = preview(tDS);
rng default;
[trainIdx,~,testIdx] = dividerand(30,0.8,0,0.2);
trainIdx_all = {};
m = 1;
for k = trainIdx
if k == 1
start = k;
else
start = ((k-1)*24)+1;
end
l = start:k*24;
trainIdx_all{m} = l;
m = m+1;
end
trainIdx_all = cell2mat(trainIdx_all)';
trainDS = subset(tDS,trainIdx_all);
testIdx_all = {};
m = 1;
for k = testIdx
if k == 1
start = k;
else
start = ((k-1)*24)+1;
end
l = start:k*24;
testIdx_all{m} = l;
m = m+1;
end
testIdx_all = cell2mat(testIdx_all)';
testDS = subset(tDS,testIdx_all);
layers = [ ...
sequenceInputLayer(8)
lstmLayer(80,OutputMode="sequence")
fullyConnectedLayer(4)
softmaxLayer
classificationLayer];
options = trainingOptions("adam", ...
MaxEpochs=100, ...
MiniBatchSize=32, ...
Plots="training-progress",...
InitialLearnRate=0.001,...
Verbose=0,...
Shuffle="every-epoch",...
GradientThreshold=1e5,...
DispatchInBackground=true);
traindata = readall(trainDS,"UseParallel",true);
rawNet = trainNetwork(traindata(:,1),traindata(:,2),layers,options);
testdata = readall(testDS);
predTest = classify(rawNet,testdata(:,1),MiniBatchSize=32);
confusionchart([testdata{:,2}],[predTest{:}],Normalization="column-normalized")
function Tsds = preprocess(inputDS)
sig = inputDS{1};
roiTable = inputDS{2};
% Remove first and last rest periods from signal
sig(roiTable.ROI(end,2):end,:) = [];
sig(1:roiTable.ROI(1,1),:) = [];
% Shift ROI limits to account for deleting start and end of signal
roiTable.ROI = roiTable.ROI-(roiTable.ROI(1,1)-1);
% Create signal mask
m = signalMask(roiTable);
L = length(sig);
% Obtain sequence of category labels and remove pronation, supination, and rest motions
mask = catmask(m,L);
idx = ~ismember(mask,{'Pronation','Supination','Rest'});
mask = mask(idx);
sig = sig(idx,:);
% Create new signal mask without pronation and supination categories
m2 = signalMask(mask);
m2.SpecifySelectedCategories = true;
% m2.SelectedCategories = [1 2 3 4 7];
m2.SelectedCategories = [1 2 3 4];
mask = catmask(m2);
% Filter and downsample signal data
sigfilt = bandpass(sig,[10 400],3000);
downsig = downsample(sigfilt,3);
% Downsample label data
downmask = downsample(mask,3);
targetLength = 12000;
% Get number of chunks
numChunks = floor(size(downsig,1)/targetLength);
% Truncate signal and mask to integer number of chunks
sig = downsig(1:numChunks*targetLength,:);
mask = downmask(1:numChunks*targetLength);
% Create a cell array containing signal chunks
sigOut = {};
step = 0;
for i = 1:numChunks
sigOut{i,1} = sig(1+step:i*targetLength,:)';
step = step+targetLength;
end
% Create a cell array containing mask chunks
lblOut = reshape(mask,targetLength,numChunks)';
lblOut = num2cell(lblOut,2);
% Output a two-column cell array with all chunks
Tsds = [sigOut,lblOut];
end
没有合适的资源?快使用搜索试试~ 我知道了~
LSTM 网络的序列到序列分类来检测 EMG 信号中的手臂运动matlab代码.zip
共1个文件
m:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 182 浏览量
2024-04-19
22:27:45
上传
评论
收藏 3KB ZIP 举报
温馨提示
1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
资源推荐
资源详情
资源评论
收起资源包目录
LSTM 网络的序列到序列分类来检测 EMG 信号中的手臂运动matlab代码.zip (1个子文件)
LSTM 网络的序列到序列分类来检测 EMG 信号中的手臂运动matlab代码
Bppproject.m 5KB
共 1 条
- 1
资源评论
Matlab科研辅导帮
- 粉丝: 3w+
- 资源: 7793
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功