Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RedFive · Sep 29, 2012 at 04:39 PM · 2ddatagraphinterpolation

How do I interpolate within a 2d dataset?

Hi, thanks in advance for the help!

For those of you familiar with matlab, I'm simply trying to perform an interp1, but I can't seem to find a similar function in javascript. I'm sure I'm just overlooking it, despite hours of research! Here's my problem:

I'm importing a text file of data into a 2d array (though I'm open to suggestions otherwise) and I would like to input an x value and return an interpolated y value.

The text file looks something like this:

5 20
6 21
7 25

etc, etc.

In matlab I would simply write:

y = interp1(xdata,ydata,x)

where x is my input and y is my result. Any ideas on how to perform this in js?

Thanks again!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RedFive · Oct 03, 2012 at 03:25 AM

I didn't really like the AnimationCurve solution Mike posted below. I suppose it could have worked, but it was not intended for what I was attempting to do -- evaluate data. So I wrote a simple function that essentially mimics what Matlab's interp1 does.

 function InterpX(x){  //linearly interpret a Y value given an X input
     arraylength = dataPairs.length - 1;  // get # of rows of my array
     //Debug.Log(arraylength);
 
     for (var i = 0; i < arraylength; i++){  //start for loop to look through each data point
             var index = i; //save current value of i for use later since using i directly fucks it up
                         
             //get x and y value for i position
             var NumA = parseFloat(dataPairs[i][0]);
             var NumC = parseFloat(dataPairs[i][1]);
             
             //get x and y value from data for i + 1 position
             if(index < arraylength){   
             var NumB = parseFloat(dataPairs[index + 1][0]);
             var NumD = parseFloat(dataPairs[index + 1][1]);}
             
             //find which data points our x input falls between
             if (NumA < x && x < NumB){
                     var DeltaAB = Mathf.Abs(NumB - NumA); //Get difference between the two x values
                     var DeltaxAB = x - Mathf.Min(NumA,NumB); //Our input minus whichever value is lowest
                     var Ratio = DeltaxAB/DeltaAB; // ratio of our input to the span between A and B
                     Debug.Log(x + ' is between ' + NumA + ' and ' + NumB);
                     
                     var DeltaCD = Mathf.Abs(NumD - NumC); //Get difference between the two y values
                     var y = (DeltaCD * Ratio) + Mathf.Min(NumC,NumD); //Linear interpolation of y
                     Debug.Log('y:' + y);
                     
                     break; //end i loop
                     }
     }
 } //end function InterpX

There may be more efficient ways to have written this function -- and I'm open to suggestions to improve it -- but all that really matters is that I can now process some data :D

I wanted to post this in case anybody else is ever looking for a similar function. Of course the prerequisite prior to calling the function is that you have imported data from a file to an array which is called dataPairs here.

Thanks again for the help, Mike! Much appreciated.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Oct 03, 2012 at 07:40 AM 1
Share

You should tick this as correct to get it off the unanswered list.

I should point out that AnimationCurves are usable to evaluate data - I use this technique frequently in game balancing algorithms, however it would not have produced exactly the same results as matlib.

avatar image
2

Answer by whydoidoit · Sep 29, 2012 at 04:55 PM

So import the data into an array of float - use float.Parse(someStringValue) to convert it.

Then use Mathf.Lerp(value1, value2, aNumberBetween0and1) to get your result.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RedFive · Sep 29, 2012 at 05:12 PM 0
Share

No, I don't think that's what I want. I read a lot about lerp, but it simply interpolates between two points based on a percentage. I.e. if your value1 = 1, value2 = 100, and aNumberBetween0and1 = 0.5, the result is 50.

I don't see how that helps me get a y value...maybe I'm missing something?

avatar image whydoidoit · Sep 29, 2012 at 05:17 PM 1
Share

Yeah you are quite right :) Not what you want at all.

Ok so I now think that you are probably after adding those coordinates to an AnimationCurve. Using the provided samples of X as time and the value as the Y from your input data. You can then interpolate to a point of any arbitrary X (or time).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate gameobject on random position on 1. a graph and 2. another gameobject 0 Answers

extract & calculate position Data 1 Answer

Defining displayed 3D content from external source? 0 Answers

Unity 2D - Primitive Mesh Interpolation 1 Answer

2D Animation does not start 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges