MATLAB Answers

System Object Variable Size Tuneable Properties

5 views (last 30 days)
Michael
Michael on 6 May 2021
Answered: jibrahim on 6 May 2021
I am new to system objects, but need to develop one to do some stream processing of incoming data. Part of the algorithm I am developing depends on buffering blocks of data for processing. I need these blocks to be overlapping for the processing. For this reason, I am trying to build a property of my system object that holds a buffer of data, from which I can cleave and process when there are a sufficient number of samples in the buffer.
The trouble I am having is deailing with variable sizes in my tuneable properties list. My object seems to run fine in Matlab, but when I try to deploy it in simulink I get size mismatch errors. The example below tries to create a u_hold proprty used to hold data sent to the step method. The object definition is at the end of this question. I try to initialize it as an empty matrix because and then add to that matrix as data comes in. I would obviously add code later to clear out u_hold when it reached a certain size, but I am not there yet.
Here is how I would call this function in Matlab - it seems to work - u_hold get concatinated.
>> blah = detection
blah =
detection with properties:
u_hold: [0×1 double]
>> out = blah([1 2 3 4]')
out =
1
2
3
4
>> out = blah([1 2 3 4]')
out =
1
2
3
4
>> blah.u_hold
ans =
1
2
3
4
1
2
3
4
If I try to throw this in Simulink and feed it audio data like this:
I get the following Simulink errors:
% An error occurred in the block 'detect_play/MATLAB System' during compile.
% Caused by:
% Simulink detected an error
% 'Size mismatch (size [1 x 1] ~= size [0 x 1]).
% The size to the left is the size of the left-hand side of the assignment.'.
%
% The error occurred for MATLAB System block 'detect_play/MATLAB System'. See line 26, column 13 in file '/Users/mshafer/Dropbox (NAU)/UAV-RT Team/CODE/SYSTEM OBJECT EXPLORATION/detection.m'. The error was detected during code generation phase.
% Start code generation report.
%
% To prevent this error, use one of the following:
% * Modify the System object to avoid code that does not support code generation.
% * Implement propagation methods in the System object code.
% Component:Simulink | Category:Block error
If anyone could help my figure out how to allow for the u_hold propery to expand in size during each call to the system object step while in Simulink, I would appreciate it!
classdef detection < matlab.System
% Public, tunable properties
properties
u_hold(:,1){mustBeNumeric}=[];
end
properties(DiscreteState)
end
% Pre-computed constants
properties(Access = private)
end
methods(Access = protected)
function setupImpl(obj)
% Perform one-time calculations, such as computing constants
end
function [y] = stepImpl(obj,u)
% Implement algorithm. Calculate y as a function of input u and
% discrete states.
obj.u_hold = [obj.u_hold;u];
y = u;
end
function resetImpl(obj)
% Initialize / reset discrete-state properties
obj.u_hold=[];
end
end
end

Accepted Answer

jibrahim
jibrahim on 6 May 2021
Hi Michael,
Your property u_hold is growing in size, which is problematic in simulink and in code generation in principal, where the compiler needs prior knoweldge of variable dimensions (or at least the maximum dimensions the variable must reach).
Before writing your own buffer, I strongly suggest you give dsp.AsyncBuffer a try. You can use it inside your own object to buffer your data. You write data to the buffer using the write method, and you read (with optional overlap) with the read method. The object supports code generation, so incorporating it into your detection object should work.
Do take a look at the documentation of dsp.AsyncBuffer. If you can't get it to work for your case, let me know and I'll be happy to help.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by