在时间间隔MATLAB中查找峰到峰值

时间:2014-10-03 11:04:09

标签: matlab for-loop

我将数据整理成2个矩阵,' time'和'数据'。我想找到“数据”第一列的峰峰值。矩阵。我试图通过遍历数据点,一次500个点,并在每个间隔内找到最大值和最小值来做到这一点。通过将两个值的绝对值相加来找到峰值。

峰= ABS(MAXV)+ ABS(MINV)

然后将这些值保存到excel电子表格(如果它们大于1500),之后可以查看它们。但是,数据矩阵有大约1798824行,这个过程非常繁琐。

有没有更好的方法来编写此代码以加快进程?循环看起来不错吗?如果' if'我试图找到一种直接进入下一次迭代的方法。条件不满足,但我不知道如何在MATLAB中这样做。

%maxV=maximum thrust value
%minV=minimum thrust value
%maxI=index of maximum thrust
%minI=index of minimum thrust
%peakV=peak thrust value
%peakT=time at which peak thrust value occurred

d=dir('*');%Creates matrix d which contains all the file names in the folder.
d=d(~[d.isdir]);% removes folders from the list (if some exist)
d={d.name}.';%removes the locations of the files, leaving only the names.
nf=numel(d);%calculates the variables in d
j=zeros([1,15]); %creates starting matrix to which the peak result, indexes and values can be added to

for i=3:nf


[time,data,sensors]=IMO_read_time(d{i},1); %executes function that reads binary and gives data in 3 matrices(time,data,sensors)

nrows=size(data,1); %number of data rows in matrix A
peakvalues=round((nrows/500)); %number of peak results for sampling frequency of 500Hz

for p=1:peakvalues %range of possible peak results
    for n=1:500:nrows %number of data points to be read(interval of 500)
        k=n+499;      %stating end value to be read in each interval
        if k<=nrows   %end value must be smaller than number of rows in data

        [maxV maxI]=max(data(n:k,1)); %finds maxV and maxI within 500 data points
        [minV minI]=min(data(n:k,1)); %finds maxV and maxI within 500 data points

        maxT=time(maxI,1); %finds time of maximum value
        minT=time(minI,1); %finds time of minimum value

        peakV=abs(maxV)+abs(minV); %finds peak to peak between maxV and minV

        if peakV>1500 


    Real_maxT=datevec(datenum(1970,1,1)+(maxT/86400)); %converts UNIX time into UPS format
    Real_minT=datevec(datenum(1970,1,1)+(minT/86400)); %converts UNIX time into UPS format


    M=[peakV maxI minI Real_maxT Real_minT]; %creates matrix M with values of interest
    format long

    Newj=[j; M]; %adds M onto matrix j to create matrix Newj
    j=Newj; %j then becomes Newj for next iteration

    ind=find(j(:,1),1,'last'); %gives the index of the last value of matrix j
    p=sprintf('A%d',ind); %gives the row number that the data will be written to in excel      spreadsheet
    xlswrite('20131221_PeakValues_trial1', M, 1, p); %saves matrix M in excel spreadsheet, sheet1, row n

        end;

        end;

    end
end

1 个答案:

答案 0 :(得分:1)

删除循环会产生以下代码块:

nrows=size(data,1); %number of data rows in matrix A
peakvalues=round((nrows/500)); %number of peak results for sampling frequency of 500Hz

dataBlocks = reshape(data(1:500*peakvalues),500,peakvalues);

[maxV,maxIdx] = max(dataBlocks);
[minV,minIdx] = min(dataBlocks);
peakV = abs(maxV) + abs(minV);

maxT = time(maxIdx + ((1:peakvalues)-1)*500,1);
minT = time(minIdx + ((1:peakvalues)-1)*500,1);

interestingIdx = (peakV>1500);

upsMaxT = datevec(datenum(1970,1,1)+(maxT(interestingIdx)/86400)); %converts UNIX time into UPS format
upsMinT = datevec(datenum(1970,1,1)+(minT(interestingIdx)/86400)); %converts UNIX time into UPS format

M = [peakV(interestingIdx)' maxIdx(interestingIdx)' minIdx(interestingIdx)' upsMaxT upsMinT];

xlswrite('20131221_PeakValues_trial1', M, 1);

附加说明:您正在循环p,但我不知道为什么会这样做。其次,您为此p稍后内部分配一个字符串值。


编辑:我没有考虑你的第一个循环,但是这个循环不会轻易删除,因为它会循环遍历要读取的文件。