FAQ: How can I resolve the error due to lookup table not allowing repeated breakpoints?
조회 수: 24 (최근 30일)
이전 댓글 표시
I'm trying to model abrupt discontinuities using lookup tables.
For example, consider this data that has abrupt discontinuities for input values 3 and 5.
breakpoints = [0 3 3 3 5 5 7]
tableData = [0 9 19 29 45 35 79]
plot(breakpoints,tableData)
If I try to use this with Simulink Lookup Table blocks or MATLAB's interp1, I get errors complaining about the breakpoints not being monotonically increasing.
For example, this code produces an error
interp1(breakpoints,tableData,2)
How can I resolve this error and model the discontinuities with lookup tables?
댓글 수: 0
채택된 답변
Andy Bartlett
2023년 7월 5일
편집: Andy Bartlett
2023년 7월 6일
A solution for making the lookup tables happy is to move the repeated breakpoints a "tiny amount".
A key basis for this approach is the fact that the data type used for the lookup table input and corresponding breakpoint will always be discrete.
Even a luxuriously fine grained data type like doubles is made up of a sequence of discrete representable values.
For example, in double we have ... 3-eps(3), 3, 3+eps(3) ... There are no representable values in the type double that are between 3 and 3+eps(3). In the software, the input range is NOT a continuum, so infinitely fast discontinuities don't really occur in the software in discrete data types.
So, each discontinuous area can just be modeled over its set of representable values.
To solve the repeated breakpoint problem, we can split up repeated breakpoints by moving one of them to the nearest neighboring representable values.
The attached function eliminateBreakpointRepeats makes those tiny moves.
Example
breakpoints = [0 3 3 3 5 5 7];
tableData = [0 9 19 29 45 35 79];
breakpointsNew = eliminateBreakpointRepeats(breakpoints);
mat2str(breakpointsNew,17)
plot(breakpoints,tableData,'b-',breakpoints,tableData,'bo',...
breakpointsNew,tableData,'k-',breakpointsNew,tableData,'kx')
xlabel('input')
ylabel('output')
legend({'Original curve'
'Original table points'
'New curve'
'New table points'},'location','NorthWest')
interp1(breakpointsNew,tableData,2)
As we see the previous error is resolved.
However, this was a simple default case where everything was using double.
Types Used by Lookup MUST be Considered
Many MathWorks users take their designs all the way to deployment on embedded real-time chips. In these cases, the data types used by lookup tables are almost never the luxurious doubles. Instead, they are the more efficient singles or the typically much more efficient fixed-point types.
Simulink Lookup Table blocks require breakpoints to be strictly monotonically increasing in the data type used for the breakpoint.
Separating repeating breakpoints in doubles would still collide as repeated breakpoints in a coarser data type like singles or most fixed-point types. So it is critical for elimination of repeats to consider the type the lookup will actually use.
The attached function eliminateBreakpointRepeats allows the data type to be used to specified and will achieve separation of breakpoints in the specified type.
Example 0
breakpointsOrig0 = [0 3 3 3 5 5 7];
breakpoints0 = eliminateBreakpointRepeats(breakpointsOrig0);
mat2str(breakpointsOrig0,17,'class')
mat2str(breakpoints0,17,'class')
Example 1
breakpointsOrig1 = [0 3 3 3 5 5 7];
data_type_specification1 = single(1); % specify type with numeric value
[breakpoints1,bpt1] = eliminateBreakpointRepeats(breakpointsOrig1,data_type_specification1);
mat2str(breakpointsOrig1,17,'class')
mat2str(breakpoints1,17,'class')
mat2str(bpt1,17,'class')
Example 2
breakpointsOrig2 = [0 3 3 3 5 5 7];
data_type_specification2 = 'half'; % specify type by name
[breakpoints2,bpt2] = eliminateBreakpointRepeats(breakpointsOrig2,data_type_specification2);
mat2str(breakpointsOrig2,17,'class')
mat2str(breakpoints2,17,'class')
mat2str(bpt2,17,'class')
Example 3
breakpointsOrig3 = [0 3 3 3 5 5 7];
data_type_specification3 = numerictype(0,8,5); % specify type by object
[breakpoints3,bpt3] = eliminateBreakpointRepeats(breakpointsOrig3,data_type_specification3);
mat2str(breakpointsOrig3,17,'class')
mat2str(breakpoints3,17,'class')
bpt3
Corresponding Changes in Table Values if Big Movement in Breakpoints
If the change in breakpoints is "sufficiently small", then the table data doesn't need to be changed.
But if the changes are "large" due to nearest representable values being "far away", then the lookup my be distorted from the original.
As an example of noticeable distortion, consider the original example, but with fixed-point type with only one bit to the right of the binary point. This type represents value are 0, 0.5, 1, 1.5, ... 127.5, so the breakpoint changes are fairly large.
The large breakpoint changes lead to noticable distortion, especially for input values 3.5 and 4.5.
breakpoints = [0 3 3 3 5 5 7];
tableData = [0 9 19 29 45 35 79];
nt = numerictype(0,8,1);
breakpointsNew = eliminateBreakpointRepeats(breakpoints,nt);
mat2str(breakpointsNew,17)
u = 0:0.5:7;
y = interp1(breakpointsNew,tableData,u);
plot(breakpoints,tableData,'b-',breakpoints,tableData,'bo',...
u,y,'kx',breakpointsNew,tableData,'rd')
xlabel('input')
ylabel('output')
legend({'Original curve'
'Original table points'
'New curve for representable inputs'
'Orig Table Data at New Breakpoints'},'location','NorthWest')
To avoid the distortion, the table data needs to change also.
tableDataNew = tableData
% 2nd breakpoint 3 moved to 2.5
iBreak = 2;
iiData = 1:2;
tableDataNew(iBreak) = interp1(breakpoints(iiData),tableData(iiData),breakpointsNew(iBreak));
% 4th breakpoint 3 moved to 3.5
iBreak = 4;
iiData = 4:5;
tableDataNew(iBreak) = interp1(breakpoints(iiData),tableData(iiData),breakpointsNew(iBreak));
% 5th breakpoint 4 moved to 4.5
iBreak = 5;
iiData = 4:5;
tableDataNew(iBreak) = interp1(breakpoints(iiData),tableData(iiData),breakpointsNew(iBreak));
u = 0:0.5:7;
yCorrected = interp1(breakpointsNew,tableDataNew,u);
plot(breakpoints,tableData,'b-',breakpoints,tableData,'bo',...
u,yCorrected,'kx',breakpointsNew,tableDataNew,'md')
xlabel('input')
ylabel('output')
legend({'Original curve'
'Original table points'
'New curve for representable inputs'
'New table points distortion corrected'},'location','NorthWest')
By adjusting the table data to agree with the new breakpoints, all the distortion was removed.
Also, notice that we're only judging the distortion for the representable inputs values (x's). There are no representable values between the x's, so distortion there is meaningless.
Critical Assumption
A critical assumption for this approach was that the lookup table is using the same data type for a breakpoint as its corresponding input, which is the default for Simulink Lookup Table blocks.
If a coarser data type was used for the breakpoints, then the input would have representable values that are between representable values in the breakpoint data type. So if you need to model discontinuities, then to avoid these issue use the default of having breakpoint and corresponding inputs use identical data types.
Summary
One way to resolve errors due to repeated breakpoints is to slightly move the breakpoints.
Simply determine the data type that will be used by the lookup table and call the attached function eliminateBreakpointRepeats. See help eliminateBreakpointRepeats for more details on usage.
If the breakpoints move a lot, then you may need to change the table data to avoid distortion.
But if you are using doubles, or even singles, then distortion is likely negligible and you can leave the table data unchanged.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Nonlinearity에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!