org-arl / SignalAnalysis.jl

Signal analysis toolbox for Julia
MIT License
42 stars 11 forks source link

fix: rand for any float types #19

Closed ymtoo closed 3 years ago

ymtoo commented 3 years ago

Errors occur when generating random Red Gaussian and Pink Gaussian noise of any float types.

julia> rand(RedGaussian(1000, convert(Float32, 1.0)))
ERROR: MethodError: no method matching _rand!(::Random._GLOBAL_RNG, ::RedGaussian{Float32}, ::Array{Float64,1})

julia> rand(PinkGaussian(1000, convert(Float32, 1.0)))
ERROR: MethodError: no method matching _rand!(::Random._GLOBAL_RNG, ::PinkGaussian{Float32}, ::Array{Float64,1})

After the changes:

julia> rand(RedGaussian(1000, convert(Float32, 1.0)))
1000-element Array{Float32,1}:
 -0.61762047
  0.026895309
 -0.112934574
  ⋮
 -0.33766067
 -0.964278
 -1.4871243

julia> rand(PinkGaussian(1000, convert(Float32, 1.0)))
1000-element Array{Float32,1}:
 1.1732337
 0.46513683
 0.6875517
 ⋮
 1.5279891
 1.1409322
 1.3683038
codecov-io commented 3 years ago

Codecov Report

Merging #19 (3f3cf76) into master (1c1d8fd) will decrease coverage by 0.03%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #19      +/-   ##
==========================================
- Coverage   79.02%   78.99%   -0.04%     
==========================================
  Files          10       10              
  Lines         615      614       -1     
==========================================
- Hits          486      485       -1     
  Misses        129      129              
Impacted Files Coverage Δ
src/rand.jl 90.90% <100.00%> (ø)
src/dsp.jl 98.02% <0.00%> (-0.02%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 1c1d8fd...3f3cf76. Read the comment docs.

baggepinnen commented 3 years ago

You may find the functions zero(T), one(T) useful in place of converting Float64 0,1 to T. zero(T) also does the right thing for arrays, for algorithms that are generic enough to work on those as well.

ymtoo commented 3 years ago

Yes, it's much cleaner to use zero(T), one(T). Thanks!