Skip to content

Commit

Permalink
🎨 Improve code structure etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
astrogewgaw committed May 2, 2023
1 parent b499b9b commit e4e15c0
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 92 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Ujjwal Panda <[email protected]>
Copyright (c) 2022-2023 Ujjwal Panda <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "Noisy"
name = "PowerLawNoise"
version = "0.0.1"
uuid = "9abb9eec-80c7-4a18-accd-d591357fead6"
authors = ["Ujjwal Panda <[email protected]>"]
Expand All @@ -8,4 +8,4 @@ FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
julia = "1.7"
julia = "1.8"
38 changes: 0 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1 @@
<div align="left" style="font-family:juliamono">
<h2>
<i>
<code>Noisy</code> :
<small><u>Simulating power law noise in Julia.</u></small>
</i>
</h2>

<br/>

![License][license]
![GitHub Stars][stars]
[![Gitmoji Badge][gitmoji_badge]][gitmoji]

<br/>

<div align="justify">

This package implements a novel algorithm, following the work in [**"*On
generating power law noise*", Timmer, J. and Koenig, M., Astron. Astrophys. 300,
707-710 (1995)**][paper], which allows generating power law noise with arbitrary
dimensions and arbitrary exponents. An exponent of 0, 1 and 2 corresponds to
white, pink and red noise. The figure below demonstrates the difference between
them, by plotting the resultant time series, generated via `Noisy.jl`:

![Plot: 1D Noise](./noisy.png)

Here is another one, but this time we are simulating noise in two dimensions:

![Plot: 2D Noise](./noisy2d.png)

</div>
</div>

[gitmoji]: https://gitmoji.dev
[paper]: https://ui.adsabis.harvard.edu/abs/1995A%26A...300..707T/abstract
[stars]: https://img.shields.io/github/stars/astrogewgaw/Noisy.jl?style=for-the-badge
[license]: https://img.shields.io/github/license/astrogewgaw/Noisy.jl?style=for-the-badge
[gitmoji_badge]: https://img.shields.io/badge/gitmoji-%20😜%20😍-FFDD67.svg?style=for-the-badge
Binary file removed noisy.png
Binary file not shown.
Binary file removed noisy2d.png
Binary file not shown.
Binary file added pix/noisy1D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pix/noisy2D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pix/spectrum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 0 additions & 51 deletions src/Noisy.jl

This file was deleted.

48 changes: 48 additions & 0 deletions src/PowerLawNoise.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Noisy

export noisy

using FFTW
using Random

zerolast!(A) = selectdim(A, ndims(A), lastindex(A, ndims(A))) .= 0.0
zerofirst!(A) = selectdim(A, ndims(A), firstindex(A, ndims(A))) .= 0.0

function σ(A, n₀, n₁)
2 * (
sum([1 < i < n₁ ? A[i]^2 : 0.0 for i eachindex(A)]) + (A[end] * (1 + (n₀ % 2)) / 2)^2,
) / n₀
end

function scales!(A, ν, ν₀, β)
for i LinearIndices(A)
A[i] = (ν[i] < ν₀ ? ν₀ : ν[i])^(-β / 2)
end
A
end

function scales(ν, ν₀, β)
A = similar(ν)
scales!(A, ν, ν₀, β)
end

function noisy(β, ν₀, dims...)
n₀ = dims[end]
ν = rfftfreq(n₀)
ν₀ = 0 <= ν₀ <= 0.5 ? max(ν₀, n₀^-1) : throw("ν₀ ∉ [0, 0.5].")

N = Array{ComplexF64}(undef, dims[1:end-1]..., size(ν, 1))

X = randn(Float64, size(N)...)
Y = randn(Float64, size(N)...)

zerofirst!(Y)
A = scales(ν, ν₀, β)
(n₀ % 2) == 0 && zerolast!(Y)
for i in CartesianIndices(N)
N[i] = A[Tuple(i)[end]] * (X[i] + (im * Y[i]))
end
irfft(N, n₀, ndims(N)) ./ σ(A, size(A, 1), n₀)
end

end

0 comments on commit e4e15c0

Please sign in to comment.