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

Respuestas (2)

Star Strider
Star Strider el 19 de Jun. de 2022

0 votos

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 comentarios

Hafsa
Hafsa el 19 de Jun. de 2022
Editada: Hafsa el 19 de Jun. de 2022
Thank you let me try it.
Attaching the txt file
I am trying to read emg signal, analyse it and than filter it using bandpass.
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
Hafsa el 19 de Jun. de 2022
close all
clear all
T1 = readtable('emg_healthy.txt', 'VariableNamingRule','preserve');
t = T1{:,1};
EMG = T1{:,2};
L = numel(t);
Fs = 1/(t(2) - t(1)); %sampling frequency
Fn = Fs/2; %cutoff frequency
figure
subplot(3,1,1)
plot(t, EMG)
grid
title('Raw EMG')
xlabel('Time')
ylabel('Amplitude')
xlim([min(t) max(t)])
% Create a butterworth filter
% Returns the lowest order
[B,A] = butter(4,Fn/(Fs/2)); % Butterworth filter design
% Convert digital filter transfer to second-order section form
emg = filtfilt(B,A, EMG);
time = 1:length(emg);
subplot(3,1,2)
plot(time,emg)
grid on
title('EMG low filtered')
%high pass filter
[X,Y]=butter(2,Fn/Fs)
high=filtfilt(X,Y,EMG)
time2 = 1:length(high);
subplot(3,1,3)
plot(time2,high)
title('EMG high pass')
This is the ERROR
EMGProjectAnother
Error using butter>butterImpl (line 85)
The cutoff frequencies must be within the interval of (0,1).
Error in butter (line 59)
[varargout{1:nargout}] = butterImpl(n,Wn,varargin{:});
Error in EMGProjectAnother (line 19)
[B,A] = butter(4,Fn/(Fs/2)); % Butterworth filter design
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
Hafsa el 19 de Jun. de 2022
Well I have a restriction to usee the butterworth low pass and high pass
I am not familiar with these yet
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
So it makes it difficult for me
If you could help in the code that I gave that would be appreciated
Star Strider
Star Strider el 19 de Jun. de 2022
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.

Iniciar sesión para comentar.

Hafsa
Hafsa el 19 de Jun. de 2022

0 votos

I am getting these graphs when I use this butterworth for low pass [B,A] = butter(4,Fn/(Fs/1))

Etiquetas

Preguntada:

el 19 de Jun. de 2022

Comentada:

el 19 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by