Author Topic: pow(a,b)  (Read 15051 times)

0 Members and 3 Guests are viewing this topic.

Online gf

  • Super Contributor
  • ***
  • Posts: 1844
  • Country: de
Re: pow(a,b)
« Reply #50 on: March 12, 2021, 08:28:13 am »
Why not simply approximate the transfer function with a polynomial, as already suggested by rstofer?
A 5th order polynomial fits nicely (see plot). I would not use a  higher order as the provided data are ovbiously noisy.
The evaluation requires 5 multiplications and 6 additions then. The coefficients can be either be stored in a table, or immediate values in the code.

Code: [Select]
>> data
data =

     2.75000    15.00000
     2.55000    20.00000
     2.00000    30.00000
     1.55000    40.00000
     1.25000    50.00000
     1.15000    60.00000
     0.90000    70.00000
     0.80000    80.00000
     0.75000    90.00000
     0.65000   100.00000
     0.60000   110.00000
     0.55000   120.00000
     0.50000   130.00000
     0.44000   140.00000
     0.45000   150.00000

>> P = polyfit(data(:,1), data(:,2), 5)
P =

   -12.951   118.581  -429.858   782.454  -750.827   357.198

>> x = 0.44:0.01:2.75;
>> plot(data(:,1), data(:,2), ".", x, polyval(P,x)); grid on
« Last Edit: March 12, 2021, 09:32:47 am by gf »
 
The following users thanked this post: DiTBho

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5128
  • Country: gb
Re: pow(a,b)
« Reply #51 on: March 12, 2021, 10:40:10 am »
Why not simply approximate the transfer function with a polynomial, as already suggested by rstofer?

Good idea! I am still studying  Octave, and I did a couple of attempts, but then I have been busy with a complete different job task.

provided data are obviously noisy.

The nature of the sensor is IR reflective, that's very noisy by itself, that's also the reason why I want to filter and to amplify the signal. Not yet done, but I will this weekend.

I am also waiting for a couple of better ADC at 12 and 16 bit of resolution. I need to write an SPI function to manage them, but hey? look at the current firmware only takes 512 byte, and it manages a mean-window filtering function, the ADC conversion, the serial talking to the host (with a minimal protocol to setup-up working variables), and a 100uS-fine timer used to control the motor.

All written in assembly. I love it!

I am going to reuse the 100uS-fine timer for a new ultrasonic ranging sensor used with quad rotors. It's modulator is smarter than traditional ultrasonic ranging sensors, and its outputs is a digital pulse of the width directly proportional to the measured distance.

I ordered  it two days ago, it will probably arrive tomorrow.

So I may use both: IR reflective + ultrasonic, or just one. We will see :D
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8354
  • Country: fi
    • My home page and email address
Re: pow(a,b)
« Reply #52 on: March 13, 2021, 03:38:15 pm »
I often use Gnuplot for fitting data to a function I define myself.

For example, if you have data.txt with input value in the first column, output value in the second column, and if there are any comments they start with a #, you can start Gnuplot, and tell it e.g.
    f(x) = C0 + x*(C1 + x*(C2 + x*(C3 + x*(C4 + x*C5))));
    fit f(x) "data.txt" using 1:2 via C0,C1,C2,C3,C4,C5
and you can also draw both the sample data and the curve fit using e.g.
    plot "data.txt" using 1:2 notitle with points lc "black", f(x) notitle with lines lc "red"
You can print the coefficients using e.g.
    print(C0, C1, C2, C3, C4, C5)
but Gnuplot will automatically save a text file called fit.log which contains the fitted variables (C0 through C5, above) under the "Final set of parameters" heading; and all kinds of convergence, error, and correlation matrix information that you may or may not find useful.

If you want the function in a variable name to be used in the plot, you can use sprintf (which has the same syntax the C printf() function has):
    fstr = sprintf("f(x) = %f + x*(%f + x*(%f + x*(%f + x*(%f + x*%f))))", C0, C1, C2, C3, C4, C5)
so that
    set title fstr ; plot "data.txt" using 1:2 notitle with points lc "black", f(x) notitle with lines lc "red"
will add the function definition as the title of the plot.

I've found this to be very useful when I am still exploring what kind of a function fits my needs most.  (In Gnuplot, AB is written A**B, and you can use conditional expressions – (condition ? value-if-true : value-if-false) – in functions. ^|& are binary operators between integers (exclusive-or, or, and, respectively), so 2^5==7 and not 32.  == tests for equalty, and != for inequality.)

As an example, when dealing with distances r, I often try to avoid r and 1/r, preferring r2 and 1/r2 instead, because if r is calculated from a vector, odd powers need a "slow" square root, whereas even powers are "cheap" (just need multiplications; and only one division for all even powers of the reciprocal of the distance).

When implementing the function in fixed point format, int(expression) is very useful, because it truncates the expression to an integer.  However, the fit variables (C0 through C5, above) must still be treated as reals, as otherwise Gnuplot cannot converge to a solution.  (This means, if you try to use int(C0) in f(x), you can expect Gnuplot to not be able to converge to a solution, because small changes in C0 do not have any effect on the result.)
 
The following users thanked this post: DiTBho

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17860
  • Country: fr
Re: pow(a,b)
« Reply #53 on: March 13, 2021, 03:51:35 pm »
For most of my "manual" (meaning not automatically generated - for this Gnuplot is nice, there are others) graphing needs, I use Scidavis. Many features including curve fitting.

 
The following users thanked this post: DiTBho

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 8354
  • Country: fi
    • My home page and email address
Re: pow(a,b)
« Reply #54 on: March 13, 2021, 04:13:47 pm »
automatically generated - for this Gnuplot is nice
Indeed, the way I usually store the derivation of the coefficients is to write the fitting (and plotting) into a script.  But mostly, it just fits my workflow.

I might be strange, but I don't use Jupyter notebooks (for Sagemath), wxMaxima documents, Octave workspaces, Maple notebooks, etc.; I prefer to save the operations into a text file.  It is faster for me to less or find+grep details to find the particular project, than to open a dozen or so of them in their respective programs to see the content.  Then again, I have hundreds of those snippets...

For proper documentation, there's LibreOffice and LaTeX and other, better tools.
 

Offline DiTBhoTopic starter

  • Super Contributor
  • ***
  • Posts: 5128
  • Country: gb
Re: pow(a,b)
« Reply #55 on: March 18, 2021, 10:05:08 pm »
I have been very busy with other stuff but I haven't forgotten this project.

Today I managed to find two hours to modify my board and to implement an new uploader software able to auto-reset the target. The RS232 signal CTS is used for this.

Code: [Select]
uploader-v1 # ./obj/binvect-uart-upld hAllo.bin
loading binvect ... done, 512 bytes loaded
opening /dev/ttyS0 at 8N1-1200bps ... success
resetting target and waiting for ack ... done
uploading: [########################################] 100%, success

It works, and it allows me to develop things remotely and to upload them to the target so I can work on it, collects data, experiment stuff, even when I am outdoors :D

This uploader also works with the chip in expanded mode. I have already prepared a new board and a special "first stage bootloader" and it's ready to work.

If I don't have to go overseas for work, we will see some progress soon.
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->