Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

comparing arrays in ruby (problem)

For general discussion related FlowStone

comparing arrays in ruby (problem)

Postby tester » Sun Dec 19, 2021 1:59 pm

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.
Attachments
comparator.fsm
(776 Bytes) Downloaded 430 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing arrays in ruby (problem)

Postby DaveyBoy » Mon Dec 20, 2021 11:44 pm

Here's my attempt:

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


Hoping one of the Ruby Gurus will be along shortly to show me how it should be done :D
User avatar
DaveyBoy
 
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

Re: comparing arrays in ruby (problem)

Postby tulamide » Tue Dec 21, 2021 12:36 am

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]
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2687
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: comparing arrays in ruby (problem)

Postby tester » Wed Dec 22, 2021 8:20 pm

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.
Attachments
autogain-equi.fsm
(1.95 KiB) Downloaded 435 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing arrays in ruby (problem)

Postby tester » Thu Dec 23, 2021 2:11 pm

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"
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing arrays in ruby (problem)

Postby tulamide » Thu Dec 23, 2021 5:32 pm

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]
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2687
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: comparing arrays in ruby (problem)

Postby DaveyBoy » Thu Dec 23, 2021 11:35 pm

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 :)
User avatar
DaveyBoy
 
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

Re: comparing arrays in ruby (problem)

Postby tulamide » Fri Dec 24, 2021 4:32 am

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.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2687
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: comparing arrays in ruby (problem)

Postby tester » Fri Dec 24, 2021 11:59 am

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.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: comparing arrays in ruby (problem)

Postby DaveyBoy » Fri Dec 24, 2021 2:44 pm

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 :)
User avatar
DaveyBoy
 
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

Next

Return to General

Who is online

Users browsing this forum: No registered users and 30 guests