How to automatically find the function that linearizes another function.

3 vues (au cours des 30 derniers jours)
Hi, I have the problem of finding the function that linearizes another function. I have hardware that measures a voltage, but the analog front end is not linear, so instead of having a linear response, I have a response like this:
What I would like to have instead is something like this, a linear function:
Now, I have many hardware, and each one has a slightly different response from each other, what I would like to do is create a Matlab script that can find the correction function that allows to have a linear response for each hardware.
That is, I would like to create a script that given this curve as input:
find the function that multiplied by that curve gives me a linear response.
Is it possible to do this with Matlab?

Réponse acceptée

Sam Chak
Sam Chak le 4 Sep 2023
Some instruments may respond nonlinearly when the measurement signal is out of the standard operating range. In your case, the raw measurement voltage () behaves nonlinearly in response to an array of known input voltage signals ().
Thus, you want to find a function g that corrects the raw measurement voltage such that it provides accurate readings in response to the known input voltage signals. This function g is the inverse function of the raw measurement voltage, .
Generally, you need to find the function f that describes the relationship between ​ and ​ via curve-fitting methods. Then, you can find the corrective function using the 'finverse' function. Check out the example below:
syms x
f(x) = 38*tanh(x/38); % found via curve-fitting method, fit()
g = finverse(f)
g(x) = 
Please take a look if this is what you want.
Vin = linspace(0, 30, 3001); % Input Voltage signal
Vmea = 38*tanh(Vin/38); % Raw Measurement Voltage
Vlin = Vin; % Expected Linear Voltage (in theory)
figure(1)
yyaxis left
plot(Vin, Vmea),
ylabel('Measured Voltage, V_{mea}')
yyaxis right
plot(Vin, Vlin, '--'), grid on
ylabel('Linear Voltage, V_{lin}')
xlabel('Input Voltage Signal, V_{in}')
title('Measured Voltage vs Expected Linear Voltage')
% Inverse function
figure(2)
Vpp = 38*atanh(Vmea/38);
plot(Vmea, Vpp), grid on
xlabel('Raw Measurement Voltage, V_{mea}')
ylabel('Post-processing Voltage, V_{pp}')
figure(3)
plot(Vin, Vpp), grid on
xlabel('Input Voltage Signal, V_{in}')
ylabel('Post-processing Voltage, V_{pp}')
  1 commentaire
Federico Massimi
Federico Massimi le 4 Sep 2023
OK, great, with this hint I managed to solve the problem. Thank you.

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 4 Sep 2023
Modifié(e) : John D'Errico le 4 Sep 2023
If, given the function f(x), your problem is simply to find a new function g(x), such that f(x)*g(x) is linear, then it is absolutely trivial.
g(x) = x / f(x)
Ok, as long as f(x) is never zero.
If, by linearization, you mean to find a transformation function g, such that g(f(x)) is linear, that becomes more difficult. As well, finding g(x), such that f(g(x)) is linear. Because in either case, you are effectively asking to find a functional inverse, and that will often be mathematically impossible. We can easily prove that, since we can make f(x) to be a polynomial model of degree 5 or higher. And we can prove a functional inverse does not exist for that general class of problems.
However, you did say the former.
" find the function that multiplied by that curve gives me a linear response."
If you want it to look like a straight line approximation to f(x), then use polyfit, to first find the linear function that approximates f(x), as p1*x+p0. Then your solution will be:
g(x) = (p1*x+p0) / f(x)
Again, the product will be as you desire.
If you want a specific solution, that perhaps deals with f(x) being zero, then you need to provide a little more information. Is f given as a general FUNCTION, or is f given as a list of numbers, thus samples from some unknown function?
  4 commentaires
Torsten
Torsten le 4 Sep 2023
Modifié(e) : Torsten le 4 Sep 2023
Looking at the first two graphs for f and g and the fact that the OP writes "What I would like to have instead is something like this, a linear function:" , I think I'm correct, but only the OP can clarify things.
John D'Errico
John D'Errico le 4 Sep 2023
Modifié(e) : John D'Errico le 4 Sep 2023
You can rewrite the question to be anything you want. But then the question becomes yours, and not what was asked. The question very clearly states:
That is, I would like to create a script that given this curve as input:
...
find the function that multiplied by that curve gives me a linear response.
The answer is exactly as I stated, with only the caveat that if f(x) is ever zero, then you need to get slightly trickier to deal with the point at zero to avoid a divide by zero.
If @Federico Massimi wishes to clarify the question, that is their choice.

Connectez-vous pour commenter.

Tags

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by