1
votes

I have been trying to write the x-axis values (peak only) to txt file using this code (this method is working).

%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

But the problem comes in here when i have modified it to plot the graph segment by segment (i manage to get this done) but the problem is not able to write the x-axis value to the txt file when my code is like this.. anything i done wrong?

%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

the result i got was the entire list of peaks even after i enter the threshold value.

1
i want to print only threshold of a certain level. if i replace input('key in the threshold ') with '>1' instead, it would print only value that is above 1.. and let's say if i got part of the segment that is not running well and the entire segment is around threshold 0.5, they would be miss out. need advice - myfriday13

1 Answers

0
votes

You need to put the input statement outside the loop, or it will be evaluated multiple times (every time all the other conditions are true). But really, there are far more efficient ways to do what you want - based on the fact that matlab likes statements to be vectorized (operate over the entire array at once). Here are a few ideas:

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

See if that helps you figure it out!

A simpler answer to the problem you stated: you need to create an inner loop over k, and make a few other changes to your code, as follows:

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

disclaimer written without access to your data, and without access to Matlab - possible there is a syntax error but should be very close