我有一个矩阵M
,其中包含从电池充电和放电中收集的大量数据。
要知道每次充放电的开始和结束的指标并计算其他一些参数(容量和能量),我做了以下代码。
如果我的数据大小很小,那么效果很好,否则就会永远这么做。 我想知道是否还有其他办法可以做到。
感谢您的帮助!
M = load(ChargeDischargeData);
Cap=[]; % capacity charged or discharged
Tab=[];% [state |StartTime | StartIndex | EndTime | EndIndex | Capacity | Energy]
State=sign(M(1,4)); % State==-1 is a charge and State==1 is discharge
i_srt=1;capacity=0;energy=0;
filt=200;% minimun point need to consider as a valid charge or discharge
for j=2:size(M,1)
if (sign(M(j,4))~=State)
if j<filt
State=sign(M(1,4));i_srt=j;
elseif j+filt>size(M,1)
Tab=[Tab;State M(i_srt,1) i_srt M(j-1,1) j-1 capacity energy];
State=sign(M(j,4));i_srt=j;capacity=0;energy=0;
elseif (sign(M(j+filt,4))==sign(M(j,4)))
Tab=[Tab;State M(i_srt,1) i_srt M(j-1,1) j-1 capacity energy];
State=sign(M(j,4));i_srt=j;capacity=0;energy=0;
end
else
capacity=capacity+abs(M(j,4))*(M(j,1)-M(j-1,1));
if State==1 %Discharging
energy=energy+abs(M(j,2)*M(j,4)*(M(j,1)-M(j-1,1))); %energy during discharge
elseif State==-1 %Charging
energy=energy+abs((M(j,3)-M(j,2))*M(j,4)*(M(j,1)-M(j-1,1))); %energy during charge
end
end
Cap=[Cap;capacity]; %create a matrix with the evolution of the capacity
end