MATLAB Answers

Allocate and acces filtered signal in struct

2 views (last 30 days)
Christian Otte
Christian Otte on 2 May 2021
Commented: Christian Otte on 2 May 2021
Hi. I'm trying to allocate filtered signals to a new struct for the reason to acces it later on in the signal processing. i'm not quite sure typing the code.
I have 3 subject with 25 recording each where each recording is 1 ecg signal, 1 seismocardiography reference signal and 1 seismocardiography smartphone signal.
num_sub = 3; % number of subjects
num_files = 25; % number of files per subject
ecg_butter = zeros(num_sub,num_files); % pre allocation filtered ecg signal
scgREF_butter = zeros(num_sub,num_files); % pre allocation filtered scg sternum signal
scgSP_butter = zeros(num_sub,num_files); % pre allocation filtered scg smartphone signal
cur_dir = pwd;
for i = 1:num_sub
cd(['S' num2str(i) '_SCG']);
files_in_folder = dir('*.txt');
for j = 1:num_files
file = files_in_folder(j).name;
data = ReadVerTxtData(file, 5);
time_index = data(:,1);
ecg_raw = data(:,3);
scg_ref = data(:,4);
scg_sp = data(:,5);
Fs = 5000;
Fn = Fs/2;
time = time_index/Fs;
wo = 50/(Fs/2);
bw = wo/35;
[b,a] = iirnotch(wo,bw);
ecg_filt = filtfilt(b, a, ecg_raw);
[b_lp, a_lp] = butter(2, 60/(Fs/2));
ecg_butter(i,j) = filtfilt(b_lp, a_lp, ecg_filt);
[b_scg, a_scg] = butter(2, [10 150]/(Fs/2));
scgREF_butter(i,j) = filtfilt(b_scg, a_scg, scg_ref);
scgSP_butter(i,j) = filtfilt(b_lp, a_lp, scg_sp);
Jan on 2 May 2021
"i'm pretty sure the problem is within the following coding" - I still do not understand, which problem you have. You tell what you want to do and post some code. But what is the problem? Do you get an error? If so, please post the complete message.

Sign in to comment.

Accepted Answer

Jan on 2 May 2021
I guess, that you get an error. I cannot check this by my own, because I do not have your input data.
ecg_raw = data(:,3);
now ecg_raw is a vector. Then:
ecg_filt = filtfilt(b, a, ecg_raw);
is a vector also. But now, you try to try to store a vector in a scalar:
ecg_butter(i,j) = filtfilt(b_lp, a_lp, ecg_filt);
% ^ this is a scalar element of ecg_butter
Do the signals have the same length? Then:
ecg_butter(:, i,j) = filtfilt(b_lp, a_lp, ecg_filt);
% With a matching pre-allocation
If the data have different lengths, use a cell array:
ecg_butter = cell(num_sub,num_files);
ecg_butter{i,j} = filtfilt(b_lp, a_lp, ecg_filt);
  1 Comment
Christian Otte
Christian Otte on 2 May 2021
You guessed right. Sorry. i'm new at matlab! I had an error (See attach)
My data don't have the same length yet, so therefore I used the cell array as recommanded. And it works fine now. thanks for your help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!