开发者

How to produce X values of a stretched graph?

I'm trying to "normalize" monthly data in a way.

What I mean by that is, I need to take daily values and check the data from each month against the data in another month.

The only problem with this is that some months are longer than others. So I have come up with a way that I want to do this, but I'm kind of confused as to exactly how to do it...

Basically, I'm looking at this website: http://paulbourke.net/miscellaneous/interpolation/ and trying to transform each set of coordinates into a graph with 31 X- and Y-values (I would like to use the Cosine interpolator, but I'm still trying to figure out what to do with it).

Now, the X values have a function. I can pass in something like (1..28) and morph it into 31 values...simple enough code, something like this works for me:

def total = (1..30)
def days = 28

def resize = {x, y->
    result = []
    x.each{ result << (it * (y/x.size())}
    return result
}

resize(total,days)

Which returns a list of 31 Y-values spanning from 0 to 28.

My question is: How do I translate a list of the corresponding Y values to these values? I'm having a really hard time wrapping my head around the concept and could use a little help.

My first thought was to simply run the Y-values through this function too, but that returns values that are all lower than the original in开发者_开发问答put.

I'm looking for something that will retain the values at certain points, but simply stretch the graph out horizontally.

For example, at the x value at (1/3) of the graph's length, the value needs to be the same as it would be at (1/3) of the original graph's length.

Can anyone help me out on this? It's got me stumped.

Thanks in advance!


Not sure where the problem lies with this, so I made up some data

I think your algorithm is correct, and you only need to normalize the x-axis.

I came up with this code (and some plots) to demonstrate what I believe is the answer

Define some x and y values:

def x = 1..30
def y = [1..15,15..1].flatten()

Then, generate a list of xy values in the form: [ [ x, y ], [ x, y ], ...

def xy = [x,y].transpose()

If we plot this list, we get:

How to produce X values of a stretched graph?

Then define a normalize function (basically the same as yours, but it doesn't touch the y value)

def normalize( xylist, days ) {
  xylist.collect { x, y -> [ x * ( days / xylist.size() ), y ] }
}

Then we can normalize our list to 28 days

def normalxy = normalize( xy, 28 )

Now, if we plot these points, we get

How to produce X values of a stretched graph?

As you can see, both plots have the same shape, they are just different widths...

Have I missed the point?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜