Custom trapezoidal function in matlab - matlab

I want to create a trapezoidal-like function in Matlab that will repeat not only for one or two periods, but for the duration of the simulation. Also, I want to control every value of the basic pulse, like the maximum value (in the picture attached it is 10000), how quickly it rises to maximum, how long it stays at maximum, and at zero. I have attached a picture of the main pulse. I have previously used a trigonometric function
Fn=((a/pi)*(asin(sin((2*pi/m)*t+l))+acos(cos((2*pi/m)*t+l)))-a/2+c)
but I cannot control all the above characteristics and therefore its not representative of what i want to do.
Picture of what I have currently
Thanks for the help in advance.

Related

Smoothing/Filtering the Real-Time Signal with Sliding Window

I am taking the datas from my sensor and the datas are the multiples of 5.8594 so my whole data looks like a mix of square wave and ramp function. What I want is, smooth the corners of this data with for loop. I want to see exponential-like or sinusoidal-like signal while iterating. I can do it with whole data but can not do it with for loop.
Thank you for your suggestions.
enter image description here
I tried low pass filtering to whole data and it works. But with windowing and real time processing, it is not applicable with this level of order.

Matlab: Comparing two signals with different time values and placed impulses

We are analysing some signals that contains an impuls in the form of a dip in the standard signal in matlab.
Signals
As you can see on the picture, we need to find the difference between the "Zlotty" and the "Krone". The two graphs besides each other, are the graphs that needs to be analyzed.
As you can see the time of the impulse is different in when it occures and in how long the impuls is. We can not use the Time as a value of measurements because that can vary randomly.
Each graph is made by vectors containing 2.5mio datapoints.
How would you use matlab to find a difference?
You could split the problem into two parts. Ensuring the same time scale for both signals and finding a possible time shift in the alignment of the resulting signals. The first part could be achieved by using the resample function of Matlab; and the second task by using cross-correlation. Using two nested for loops, you could perform a search for the "best" stretch factor and time shift that result in the maximum correlation coefficient.

Combination of two signals to determine the end signal sent through Simulink code

Goal
I'm trying to have a second signal (caused by force applied to a sensor's surface area), once it reaches a set minimum, to determine a servo motor's position. The motor's position is otherwise determined by a first signal (generated by EMGs that are measured with electrodes placed on a subject's body) that make give the servo preset positions when the signal crosses a certain threshold.
Schematic
I cannot seem to get the two signals to work together. Any corrections and recommendations on how to make the logic within Simulink function as intended would be immensely appreciated. I know little of the program and cannot find a good approach.
In your diagram you go from numeric values (Sensors) to logical ones (>=301) back to numeric (*90) and again to logical (AND). What you are doing may be possible but hides the intention. First of all I would recommend you to clearly distinguish between logical conditions and values, similar to the following model. To help you with that you can turn on data-type display from Format > Port/signal display > Signal data types followed by an Update (Ctrl-D).
Alternatively you could reduce the conditions as much as possible and operate only with values, such as
However I do not fully understand your requirements and I'm also not sure what the input of the servo is representing (Speed, position, ...)? Maybe you could explain the desired behaviour a bit more detailed? In particular how do you come up with the limits (301, 300, ...).

3rd-order rate limiter in Simulink? How to generate smooth triggered signals?

First for those, who are not familiar with Simulink, there is a imaginable outside-Simulink partial solution:
I need to create a vector satisfying the following conditions:
known initial value a1
known final value a2
it has a pre-defined step size, but the length is not pre-determined
the first derivative over the whole range is limited to v_max resp. -v_max
the second derivative over the whole range is limited to a_max resp. -a_max
the third derivative over the whole range is limited to j_max resp. -j_max
at the first and the final point all derivatives are zero.
Before you ask "what have you tried so far", I just had the idea to solve it outside Simulink and I tried the whole stuff below ;)
But maybe you guys have a good idea, while I keep working on my own solution.
I'd like to generate smooth ramp signals (3rd derivative limited) based on a trigger signal in Simulink.
To get a triggered step I created a triggered subsystem propagating the trigger output. It looks like that:
But I actually don't want a step, I need a very smooth ramp with limited derivatives up to the 3rd order. The math behind is:
displacement: x
speed: v = x'
acceleration: a = v' = x''
jerk: j = a' = v'' = x'''
(If this looks familiar to you, I once had a very similar question. I thought about a bounty on it, but after the necessary edit of the question both answers would have been invalid)
As there are just rate limiters of 1st order, I used two derivates and a double integration to resolve my problem. But there is a mayor drawback, I can not ignore anymore. For the sake of illustration I chose a relatively big step size of 0.1.
The complete minimal example (Fixed Step, stepsize: 0.1, ode4): Download here
It can be seen, that the signal not even reaches the intended step height of 10 and furthermore is not constant at the end.
Over the development process of my whole model, this approach was satisfactory enough for small step sizes. But I reached the point where I really need the smooth ramp as intended. That means I need a finally constant signal at exactly the value, specified by the step height gain.
I already spent days to resolve the problem, and hope to fine some help here now.
Some of my ideas:
dynamically increase the step height over the actual desired value and saturate the final output. If the rate limits,step height and the simulation step size wouldn't be flexible one could probably find a satisfying solution. But as everything has to be flexible, there are too much cases where the acceleration and jerk limit is violated.
I tried to use the Matlab function block and write my own 3rd order rate limiter. Though it seems possible for me for the trigger moment, I have no solution how to smooth the "deceleration" at the end of the ramp. Also I'd need C-compilers, which would make it hard to use my model on other systems without problems. (At least I think so.)
The solver can not be changed siginificantly (either ode3 or ode4) and a fixed step size is mandatory (0.00001 to 0.01).
Currently used, not really useful approach:
For a dynamic amplification of 1.07 I get the following output (all values normalised on their limits):
Though the displacement looks nice, the violation of the acceleration limit is very harmful.
For a dynamic amplification of 1.05 I get the following output (all values normalised on their limits):
The acceleration stays in its boundaries, but the displacement does not reach the intended value. (not really clear in the picture) The jerk is still to big. (I could live with that, but it's not nice)
So it appears to me that a inside-Simulink solutions is far from reality. Any ideas how to create a well-behaving custom function block?
Simulation step size, step height, and the rate limits are known before the simulation starts. (But I have a lot of these triggered smooth ramps in a row, it should feed a event-discrete control). So I could imagine to create the whole smooth ramp outside simulink and save it as a timeseries object and append it on the current signal when the trigger is activated.
The problems you see are because the difference is not conditioned very well.
Taking the difference amplifies the numerical that exists in your simulation.
Also the jerk will always be large if you try to apply an actual step.
I guess for your approach it would be better to work the other way around:
i.e. make a jerk, acceleration and velocity with which your step is achieved.
I think your looking for something like the ref3 block:
http://www.dct.tue.nl/home_of_ref3.htm
Note the disclaimer on the site and that it is a little cumbersome to use.
An easy (yet to be improved) way is to use a rate limiter and then a state space model with a filter. From the filter you get the velocity, which in turn you can apply a rate limiter to. You continue with rate-limiter and filters until you have the desired curve.
Otherwise you can come up with numerical rate-limiters of higher order using ie runge kutta formulas or finite differences. However it was pointed out, that they may suffer from bad conditioning.
What I usually do is to use one rate limiter and a filter of 3rd Order and just tune the time constant (1 tripple pole), such that my needs are met. This works well, esp
Integrator chains of length > 1 are unstable!
There is a huge field of research dealing with trajectory planning. The easiest way might be to use FIR filters (Biagotti et al) or to implement an online trajectory planner (Ezair et al 2014 / Knierim et al 2012).

finding the global maximum of an unknown surface

I have a model that is solved, returns a single output value and plots it. From those values, I plot a surface using x-values varying from 1-35 and y-values varying from 1-39, and have the returned values as values on the z-axis. See below.
This figure does not behave according to a defined function, it is simply a plot of output values.
I've been trying to use a random optimization algorithm that I created in an attempt to find the global maximum, but it takes a very long time and isn't always correct (when compared to a grid-search algorithm that I use as a comparison). The surface that is created has subtle changes in it, enough to create multiple troublesome local minima and maxima. I'm looking for a way to find the global maximum of this non-convex surface in a relatively quick fashion.
EDIT:
35-by-39 is the search area and that's as big as it gets. The values of the x and y axes are the input values of the model (probably shouldve mentioned that), so each of the z-values are associated with an x and y input coordinate. And my initial guess is usually smack dab in the middle of the search area.
The creation of this figure took about 50 minutes, because each of the 1365 z-values takes about 3 seconds to compute. I'd like to do this without having to use exhaustive enumeration (evaluating every point for a z-value). I'd like this to take around 5 minutes instead of 50.
EDIT(2):
Sorry for the confusion. The figure below is a 35-by-39 grid of z-values and is used purely for reference. In the actual executing of the program, all I have is the x- and y-coordinates, and I am trying to find the global maximum z-value in the fewest function evaluations possible in order to save time. So horchler, in reference to your comment, the latter.
EDIT(3):
The thing with this figure is its only a single example. There are multiple different figures that are formed when I use data from a separate source (i.e. the left side might be uninteresting in this example, but for a separate set of data, it may or may not contain the global max). And this adds to the complexity. It is impossible to tell from the data where the location of the global max will be.
Some surfaces are incredibly smooth, others have large and frequent peaks throughout.