I have a project going where i use a vue-slider for adjusting volume, and at the same time i also use vue-slider`s process to show current level on the actual output, however i have a mathematical problem thats above my head of thinking.
Lets say i have read a level of -15db, now my scale is going from -40db to +20db, however vue-slider`s process does not care about this value, it value is based on percentage, from 0 - 100.
So the question is, what equation can i use to get percentage(between 0-100%) of value(-15) within min(-40) and max(20)?
Example of the slider:
Solution i used are following:
let percent = 0
if (level > chGainMin) {
const translator = Math.abs(chGainMin)
const newMin = chGainMin + translator
const newMax = chGainMax + translator
const newLevel = level + translator
percent = ((newLevel-newMin)*100) / (newMax-newMin)
}
Using abs to turn negative minimum to a positive number, and append this number to min, max and value. Then calculate the percentage as usual.
Related
I'm trying to solve a problem where I know a percentage (set by the user) but I need to find where that percentage is on a range of numbers, so for instance if the user sets it to 50% and the value range is -10 to 10 the value would be 0.
It seems simple in my head but I'm tripping up on this one.
This should work
pos = low + (high - low) * percentage
I have a ractangle whose ratio is 80:40 and other rectangles with similar aspect ratios within a humbral for example 80:35, 85:45, that unbral is a decimal or integer.
My problem is that I need to compare other rectangles with the first one and determine a difference in their respective aspect ratios in percentages, for example: 80:30 is 20% different in aspect ratio than 80:40.
(20% is not a calculated data, it is an example idea, because I don't know how to do it).
Could it be that a totally opposite aspect ratio is 100% different? For example: 80:40 is 100% different than 40:80
Imagine that you have a collection of rectangles and a target rectangle and you have to filter the collection leaving only those rectangles that have an aspect ratio similar to the target rectangle.
Sample:
private float GetDiffRatio(FloatSize size1, FloatSize size2) {
float fixedR1 = size1.Width / size1.Height;
if (fixedR1 >= 0)
// Invert and negative.
fixedR1 = -(size1.Height / size1.Width);
float fixedR2 = size2.Width / size2.Height;
if (fixedR2 >= 0)
// Invert and negative.
fixedR2 = -(size2.Height / size1.Width);
float rDiff = fixedR1 - fixedR2;
return rDiff * 100f;
}
Test:
float diffRatio = GetDiffRatio(new FloatSize(100f, 50f), new FloatSize(50f, 100f));
Results = -100f
Test2 (inverted order of parameters):
float diffRatio = GetDiffRatio(new FloatSize(50f, 100f), new FloatSize(100f, 50f));
Results = 100f
I am not sure that this is a valid or correct form, I do not know if it can generate any condition that returns a wrong percentage of similarity.
The answer to this question depends a lot on what exactly you're trying to do or you're planning to uses this similarity function for. I find it very unintuitive to say that opposite aspect ratio leads to 0% similarity. I think comparing two rectangles r1=(2.1,2) and r2=(2,2.1) should be a lot more similar to each other then for example r3=(1,5) r4=(5,1).
This is not me saying that it couldn't be useful in some case to have a similarity function like your's, I just want to explain that it depends a lot on what you're doing ...
I would say a very obvious solution would be to just divide width by height of every rectangle and take as similarity-function s1 the absolute value from both values subtracted. So for my provided examples the result would be:
s1(r1,r2) = | 2.1/2 - 2/2.1 | = 0.0976...
s1(r2,r3) = | 1/5 - 5/1 | = 4.8
If it is also important that you have values between 0 and 1 you could additionally plug this values in for example this function ...
where
b must be smaller than 0 and is a parameter with what you can controll how fast the funciton converges to 1. YOu can play around with it here: https://www.desmos.com/calculator/nwlq3ujouq
In the case you really want something as you suggested, i would simply do the following:
You take your constraint that every rectangle is between the ratio a:b and c:d. Than you calculate x1=a/b and x2=c/d and then you interpolate the value from zero to one between those values so:
h(x1) = 0
h(x2) = 1
if you need more details on how to do this look here https://en.wikipedia.org/wiki/Linear_interpolation but i think it's very straight forward.
The similarity function s3 can then be build again with the absolute value of the difference
s3=| h(r1)-h(r2) |
I would just like to start by saying my calculus is terrible and I have next to no experience with using it.
I am trying to find an algorithm to help scaling in my game. Specifically it should scale the amount of waves that spawn per level. Ideally it will take any number as a level up to the max integer value. There would also be a minimum value and a maximum value that would be the minimum waves and maximum waves. So:
level = 0 to infinity
minValue = 3
maxValue = 40
result = an algorithm that will have a max curvature of the max value and shouldnt exceed it no matter what value the level is. I'm not sure how to calculate this but I think it would also need some kind of threshold that i could control to dictate the curvature based on the the level.
Try the next approach:
mult = Min(1, (level/MaxLevel)**Somepower))
minValue + (maxValue - minValue) * mult
Choose Somepower value suitable for your tasks. For example, value 2 gives parabola (note that value might be less than 1)
If you want more complex curve, show a picture of desired form.
Edit:
For the case when curve tends but does not become above some level, you can choose some function with horizontal asymptote. For example:
max * x /(x+1)
or
max * arctan(k*x) * 2 / Pi
I have some working code that does amplitude modulation and plots it.
However I'm trying to change the way the modulation looks (the y variable)
so it looks like an egg shape. I found an equation/website that looks good
http://www16.ocn.ne.jp/~akiko-y/Egg/index_egg_E.html
but I'm not to sure how to convert that into matlab/octave code to change the y variable
%test_amplitude modultaion
fs=1000;
t=linspace(0,2*pi,fs);
mt=1*sin(100*t); %signal you want to use
y=mt.*(1+cos(1*t+pi));%modulation equation, use pi to shift over 90 deg to start at 0
y=y';
y_norm=(y(:,1)/max(abs(y(:,1)))*.8); %normalize signal
plot(y_norm)
PS: this is matlab/octave code
Using the equations given on the page you linked:
a = 2*pi;
b = a; % change this depending on the shape of the egg you want
mt=1*sin(100*t); %signal you want to use
y = mt.*sqrt((a-b)-2*t + sqrt(4*b*t + (a-b)^2)).*sqrt(t)/sqrt(2); % modulation
The rest of your code is A-OK, although I would probably use plot(t,y_norm) at the end.
I am trying to plot large amounts of points using some library. The points are ordered by time and their values can be considered unpredictable.
My problem at the moment is that the sheer number of points makes the library take too long to render. Many of the points are redundant (that is - they are "on" the same line as defined by a function y = ax + b). Is there a way to detect and remove redundant points in order to speed rendering ?
Thank you for your time.
The following is a variation on the Ramer-Douglas-Peucker algorithm for 1.5d graphs:
Compute the line equation between first and last point
Check all other points to find what is the most distant from the line
If the worst point is below the tolerance you want then output a single segment
Otherwise call recursively considering two sub-arrays, using the worst point as splitter
In python this could be
def simplify(pts, eps):
if len(pts) < 3:
return pts
x0, y0 = pts[0]
x1, y1 = pts[-1]
m = float(y1 - y0) / float(x1 - x0)
q = y0 - m*x0
worst_err = -1
worst_index = -1
for i in xrange(1, len(pts) - 1):
x, y = pts[i]
err = abs(m*x + q - y)
if err > worst_err:
worst_err = err
worst_index = i
if worst_err < eps:
return [(x0, y0), (x1, y1)]
else:
first = simplify(pts[:worst_index+1], eps)
second = simplify(pts[worst_index:], eps)
return first + second[1:]
print simplify([(0,0), (10,10), (20,20), (30,30), (50,0)], 0.1)
The output is [(0, 0), (30, 30), (50, 0)].
About python syntax for arrays that may be non obvious:
x[a:b] is the part of array from index a up to index b (excluded)
x[n:] is the array made using elements of x from index n to the end
x[:n] is the array made using first n elements of x
a+b when a and b are arrays means concatenation
x[-1] is the last element of an array
An example of the results of running this implementation on a graph with 100,000 points with increasing values of eps can be seen here.
I came across this question after I had this very idea. Skip redundant points on plots. I believe I came up with a far better and simpler solution and I'm happy to share as my first proposed solution on SO. I've coded it and it works well for me. It also takes into account the screen scale. There may be 100 points in value between those plot points, but if the user has a chart sized small, they won't see them.
So, iterating through your data/plot loop, before you draw/add your next data point, look at the next value ahead and calculate the change in screen scale (or value, but I think screen scale for the above-mentioned reason is better). Now do the same for the next value ahead (getting these values is just a matter of peeking ahead in your array/collection/list/etc adding the for next step increment (probably 1/2) to the current for value whilst in the loop). If the 2 values are the same (or perhaps very minor change, per your own preference), you can skip this one point in your chart by simply adding 'continue' in the loop, skipping adding the data point as the point lies exactly on the slope between the point before and after it.
Using this method, I reduce a chart from 963 points to 427 for example, with absolutely zero visual change.
I think you might need to perhaps read this a couple of times to understand, but it's far simpler than the other best solution mentioned here, much lighter weight, and has zero visual effect on your plot.
I would probably apply a "least squares" algorithm to obtain a line of best fit. You can then go through your points and downfilter consecutive points that lie close to the line. You only need to plot the outliers, and the points that take the curve back to the line of best fit.
Edit: You may not need to employ "least squares"; if your input is expected to hover around "y=ax+b" as you say, then that's already your line of best fit and you can just use that. :)