Home > database >  Spacing points per decade for logarithmic plot
Spacing points per decade for logarithmic plot

Time:10-11

I'm trying to space out a number of points in between a start and end frequency. In a way you can see here down below:

Startfreq = 1 Hz ( variable ) Stopfreq = 5402 Hz ( also variable ) stepsperdecade How i want it to look: 1 - 2 - 3 - 4.. 10 - 20 - 30..100 - 200 - 300.. 1000 - 2000 - 3000 - 4000 - 5000 - 5402 OR 1 - steps based on the stepsperdecade - 10 - steps based on the stepsperdecade - 100 .. 1000 - steps based on the stepsperdecade 5402.

SO i want the spacing to be same until it reaches the end frequency I tried to do it in the following way in python.

from math import log10
import numpy as np

startfreq = 1
endfreq = 10000
points_per_decade = 10

numberdecades = log10(endfreq) - log10(startfreq)
print(numberdecades)
points = int(numberdecades) * points_per_decade
points = np.logspace(log10(startfreq), log10(endfreq), num=points, endpoint=True, base=10)
print(points)

But this way doesn't give me the 10 - 100 - 1000 i want in between the steps. Would any one know or could someone hint me in the right direction.

CodePudding user response:

I don't know if this works for you but using some basic maths I created this while loop snippet

from math import log10

startfreq = 1
endfreq = 5402
points_per_decade = 10

points = [startfreq]
ndig = int(log10(startfreq))
point = startfreq - startfreq % 10 ** ndig   10 ** ndig
while point < endfreq:
    points.append(point)
    ndig = int(log10(point))
    point = round(point   10 ** ndig, ndigits=-ndig)
points.append(endfreq)

print(points)

I edited the answer to fix certain values, like startfreq = 175 should produce 200 as the next value, then continue in steps of 100: [175, 200, 300...]

CodePudding user response:

You could do this comfortably with numpy arrays, by taking an outer product:

import numpy as np
exponents = np.arange(0, 4)# -> [0, 1, 2, 3]
prefactors = np.arange(1, 10)# -> [1, 2, ..., 9]
factor_matrix = np.outer(10**exponents, prefactors)

This will give you what you want in matrix form:

[[1, 2, ..., 9],
 [10, 20, ..., 90],
  ...,
 [1000, 2000, ..., 9000]]

Of course, you want a flat array that stops before endpoint=5402, then append endpoint manually:

flattened_array = factor_matrix.flatten()
flattened_array = flattened_array[flattened_array<endpoint]
flattened_array = np.append(flattened_array, endpoint)
  • Related