Error Index in position 2 exceeds array bounds
이전 댓글 표시
Trying to read a file and plot
This error keeps occuring
Error in EMG (line 9)
plot(data1(:,1), data1(:,2)-mean(data1(:,2)));
CODE
%% Step1 : Read Data from .txt tile
fq = 25; %sampling frequency
input_file ='emg_healthy.txt';
data1 = textread(input_file,'%s');
figure;
plot(data1(:,1), data1(:,2)-mean(data1(:,2)));
xlabel('Time/s','fontsize', 14); ylabel('Signal magnitude', 'fontsize', 14);
title('Raw Data from EMG - Biceps 1', 'fontsize', 14);set(gca,'FontSize',14);
Please help
답변 (2개)
Star Strider
2022년 6월 19일
0 개 추천
I don’t have ‘input_file’ however according to this lilne:
data1 = textread(input_file,'%s');
the ‘data1’ variable is a column vector (not a matrix) of character variables. There is no second (or further) column dimension to a column vector, and '%f' would be a more appropriate format descriptor for it. (There are also more appropriate functions to use to read it.)
댓글 수: 6
This should get you started —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1038450/emg_healthy.txt', 'VariableNamingRule','preserve');
t = T1{:,1};
EMG = T1{:,2};
L = numel(t);
Fs = 1/(t(2) - t(1));
Fn = Fs/2;
figure
plot(t, EMG)
grid
xlabel('Time')
ylabel('Amplitude')
xlim([min(t) max(t)])
NFFT = 2^nextpow2(L); % For Efficiency
FT_EMG = fft(EMG-mean(EMG),NFFT)/L; % Subtract 'mean' TO See Other Peaks
Fv = linspace(0, 1, NFFT/2+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
plot(Fv, abs(FT_EMG(Iv))*2)
grid
xlabel('Frequency')
ylabel('Amplitude')
title('EMG: Fourier Transform')
xlim([min(Fv) max(Fv)])
Use the information from the Fourier transform plot to design the filter.
.
Hafsa
2022년 6월 19일
If you have R2018a or later, use the bandpass function with 'ImpulseResponse,'iir'. It is just easier.
If you do not have bandpass, do this:
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1038450/emg_healthy.txt', 'VariableNamingRule','preserve');
t = T1{:,1};
EMG = T1{:,2};
Fs = 1/(t(2) - t(1));
Fn = Fs/2;
lowcutoff = 0.1; % Choose The Correct Frequency
highcutoff = 1.2; % Choose The Correct Frequency
Wp = [lowcutoff highcutoff]/Fn; % Passband Frequency (Normalised)
Ws = [0.95 1.05].*Wp; % Stopband Frequency (Normalised)
Rp = 1; % Passband Ripple
Rs = 60; % Passband Ripple (Attenuation)
[n,Wp] = ellipord(Wp,Ws,Rp,Rs); % Elliptic Order Calculation
[z,p,k] = ellip(n,Rp,Rs,Wp,'bandpass'); % Elliptic Filter Design: Zero-Pole-Gain
[sos,g] = zp2sos(z,p,k); % Second-Order Section For Stability
figure
freqz(sos, 2^16, Fs) % Filter Bode Plot
EMGfilt = filtfilt(sos,g,EMG);
figure
plot(t, EMGfilt)
grid
xlabel('Time')
ylabel('Amplitude')
title('Filtered EMG Signal')
xlim([min(t) max(t)])
Use the cutoff frequencies you want, and go from there. My code does the rest.
.
Hafsa
2022년 6월 19일
Star Strider
2022년 6월 19일
If you have to use the Butterworth filters, then please read the documentation on the buttord and butter functions in detail to understand their arguments, especially with respect to the frequency arguments. (I have no idea what frequencies you want to use.)
This makes no sense:
[B,A] = butter(4,Fn/(Fs/1))
The frequency argument should be the frequency in Hz (in this instance) divided by the Nyquist frequency, ‘Fn’. The lowpass filter is the default design, so for the highpass filter youi will need to use the additional argument 'high' to designate an highpass filter for it. See the documentation for details.
Then use my code as a guide to understand how to use them to get the result you want. Remember to use ‘EMG’ as the input to the first filter, and that output as the input to the second filter.
카테고리
도움말 센터 및 File Exchange에서 Butterworth에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




