Page 1 of 2

comparing arrays in ruby (problem)

Posted: Sun Dec 19, 2021 1:59 pm
by tester
From what I see, I need a solution to compare arrays (some clean compact code) in ruby in a way shown in the schematic.

Basically, there is an input array and reference table.
Each input value shoould be compared with reference table, and on output I'd like to get the index of the reference value, that meets desired criteria (i.e. index of a closest lowest value).
Thus, on output, there would be indexes of closest lower values from the reference table.

In second module, I'd like to extract the reference values at given indexes.

Thanks in advance.

This is a part of a larger schematic, which I will post after it's finished.

Re: comparing arrays in ruby (problem)

Posted: Mon Dec 20, 2021 11:44 pm
by DaveyBoy
Here's my attempt:

Nearest Lowest v3.06.fsm
(526 Bytes) Downloaded 783 times


Hoping one of the Ruby Gurus will be along shortly to show me how it should be done :D

Re: comparing arrays in ruby (problem)

Posted: Tue Dec 21, 2021 12:36 am
by tulamide

Code: Select all

a = [100, 200, 350, 400, 420, 500] ##reference
b = [75, 160, 348, 350, 409] ##values
c = [] ##result

b.each do |item|
  ##find minimum distance (=closest value) and add it to result
  c << a.min { |a,b| (a - item).abs <=> (b - item).abs }
end

c #== [100, 200, 350, 350, 400]

Re: comparing arrays in ruby (problem)

Posted: Wed Dec 22, 2021 8:20 pm
by tester
Thank you.

Attaching equal-loudness autogain formulator. Inside you will find a simple table for equal-loudness curve, and frequency-to-gain converter with linear interpolation. Such table can be more dense, and detailed in areas of larger dynamics, but it depends on user what they want. Interpolation acts between data points, so no need for hard science.

Re: comparing arrays in ruby (problem)

Posted: Thu Dec 23, 2021 2:11 pm
by tester
tulamide, how in your case get not values, but indexes of these values?

Also, the logic is broken. It should find only closest lower value, to get it's index. so for 120 and 190 it will be index id of "100"

Re: comparing arrays in ruby (problem)

Posted: Thu Dec 23, 2021 5:32 pm
by tulamide
I see.

But why would you want the index and not the reference value? It also has the advantage that you get the real closest value. However, to get the index, see below. Careful, it obviously can't find, what doesn't exist. In that case it returns nil.

Code: Select all

a = [100, 200, 350, 400, 420, 500] ##reference
b = [75, 160, 348, 350, 409, 201] ##values
c = [] ##result

b.each do |item|
  ##find lowest index of reference that's less than value
  c << a.rindex { |ref| ref <= item }
end

c #== [nil, 0, 1, 2, 3, 1]

Re: comparing arrays in ruby (problem)

Posted: Thu Dec 23, 2021 11:35 pm
by DaveyBoy
ahhh! rindex, I should have thought of that, though i've never used it before . . very useful.

Tulamide, I'm curious why you are pushing to array rather than mapping, is it more efficient? faster? :?

Thanks in advance :)

Re: comparing arrays in ruby (problem)

Posted: Fri Dec 24, 2021 4:32 am
by tulamide
DaveyBoy wrote:Tulamide, I'm curious why you are pushing to array rather than mapping, is it more efficient? faster? :?

I chose it just for the demonstration purpose here. By seperating the important bit, it (to my mind) becomes more obvious, that you can use this just as well outside of an enumerable, for example as a method.

Map, as far as I know, does the exact same thing. Creating an array and filling it with the results of the block. I don't think either one is particularly faster.

Re: comparing arrays in ruby (problem)

Posted: Fri Dec 24, 2021 11:59 am
by tester
tulamide,

By getting index of a certain value (that meets specific criteria), you can easily build a matrix of reference indexes, and then you can get various different kind of values (correlated but from different pools) at thexe indexes. So this is more generalized and scalable approach.

By getting individual values through (more complex?) computation, it's less efficient, and you need to approach each value independently.

In case of this simple equal-loudness autogain compensator - getting single index, gives you a matrix of 4 idenxes, that operate on 2 correlated tables.

As for efficiency, there is no problem if the input value just changes into something else, but the issue get's more pronounced if the input changes live at some speed (each step = full computation), and blue scenery is extensively active.

*

As for second part of your question, look inside the interpolator.

Input - bottom_neighbour / top_neighbour - bottom_neighbour = output - bottom_neighbour2 / top_neighbour2 - bottom_neighbour2

So the output follows the input curve, and the curve doesn't need to have equally spreaded points in this case.

Reference table is always present, array of values to compare too, because it's hardwired.

Re: comparing arrays in ruby (problem)

Posted: Fri Dec 24, 2021 2:44 pm
by DaveyBoy
tulamide wrote:Map, as far as I know, does the exact same thing.


Thanks T . . . I never stopped to think about how Ruby works behind the scenes :)