1

我一直在尝试使用此代码将 x 轴值(仅限峰值)写入 txt 文件(此方法有效)。

%determine the bpm
%count the dominant peaks in the signal
fid = fopen('y1.txt','a'); %txt naming and append
beat_count = 0;
for k = 2 : length(pbcg)-1
    if (pbcg(k) > pbcg(k-1) && pbcg(k) > pbcg(k+1) && pbcg(k) > 1)
        beat_count = beat_count + 1;
    end
end

fprintf(fid, '%i\n', k); %open writer
fs = 100; %freq 100hz
N = length(pbcg);
duration_in_seconds = N/fs;
duration_in_minutes = duration_in_seconds/60;
BPM_avg = beat_count/duration_in_minutes;
fclose(fid); %close writer

但是当我修改它以逐段绘制图形时问题就出现了(我设法完成了这个)但是当我的代码是这样时,问题无法将 x 轴值写入 txt 文件。 . 我做错了什么?

%plot segment by segment
data = pbcg;%data value from 1 to 10000
rows = reshape(data, 1000, numel(data)/1000)';%reshape the data into
%matrix by 1000 against total num of element in array and then / 1000)

fid = fopen('y1.txt','a'); %txt naming and append
beat_count = 0;

for e = 1:size(rows,1),
    %plot normal & with nodes together
    figure,plot(rows(e,:)),hold on,plot(rows(e,:),'ro');

    %if statement to find peak
    if (pbcg(k) > pbcg(k-1) && pbcg(k) > pbcg(k+1) && pbcg(k)> input('Key in the threshold value: '))
        beat_count = beat_count + 1;
        peaks(beat_count)=pbcg(k);
    end

    pause;%pause, on keypress go to next plot

    fprintf(fid,  'x_axis%i\n ', peaks); %open writer
end
fclose(fid); %close writer

即使在我输入阈值之后,我得到的结果也是整个峰值列表。

4

1 回答 1

0

您需要将该input语句放在循环之外,否则它将被多次评估(每次所有其他条件都为真)。但实际上,有更有效的方法来做你想做的事——基于 matlab 喜欢将语句向量化(一次对整个数组进行操作)这一事实。这里有一些想法:

rows = reshape( data, 1000, []); % the [] means "figure out what this dimension has to be so it works"

d1 = diff( rows, 1 ); % take "first derivative" along first non-singleton dimension
pkLocs = find(d1(1:end-1,:)>0 & d1(2:end,:) < 0 ); % peak = where derivative changes sign
threshold = input('Key in the threshold value:');
[pk_i pk_j] = ind2sub( size(d1), pkLocs );
pkVals = rows( sub2ind( size(rows), pk_i, pk_j) );
aboveThr = find( pkVals > threshold );
goodPk_i = pk_i( above_thr );
goodPk_j = pk_j( above_thr );

for j = 1:size(rows, 2)
  fprintf( 1, 'peaks found for segment %d:\n' );
  fprintf( 1, '%f  ', goodPk_i(goodPk_j == j ) );
end

看看能不能帮你搞清楚!

对您所说的问题的一个更简单的答案:您需要创建一个内部循环k,并对您的代码进行一些其他更改,如下所示:

beat_count = 0; 
for e = 1:size(rows,1),
    %plot normal & with nodes together
    figure,plot(rows(e,:)),hold on,plot(rows(e,:),'ro');
    threshold = input('Key in the threshold value to use: ');

    % loop over this if statement to find peaks in this row
    for k = 2 : 999
      if (rows(e,k) > rows(e, k-1) && rows(e,k) > rows(e,k+1) && rows(e,k) > threshold)
        beat_count = beat_count + 1;
        peaks(beat_count)=rows(e,k);
        peak_x(beat_count) = k + 1000 * (e - 1);
      end
    end
    fprintf(1, 'press any key to continue!\n');
    pause; % pause, on keypress go to next plot
end

% since peaks array keeps growing, we should print it out all at once:
fprintf(fid, 'the following peaks were found:\n');
for ii = 1:beat_count
  fprintf(fid, 'x = %d; peak = %f\n ', peak_x(ii), peaks(ii)); %open writer
end
fclose(fid); % close the file once you're done

免责声明无需访问您的数据,也无法访问 Matlab - 可能存在语法错误,但应该非常接近

于 2013-07-10T03:07:43.610 回答