Skip to content

Instantly share code, notes, and snippets.

View lnacquaroli's full-sized avatar

Leandro Acquaroli lnacquaroli

View GitHub Profile
@lnacquaroli
lnacquaroli / multipleReflections.jl
Last active September 1, 2019 02:38
Computes the reflection of a sequence n of refractive indexes wihtout interference and absorption effects
"""
Computes the reflection of a sequence of indices of refraction without interference and absorption effects.
y = multipleReflections(n)
author: Cuchu & lnacquaroli
"""
function multipleReflections(n::Array{Real})
R1::Float64 = 0.0
R::Float64 = 0.0
for i = 1:length(n)-1
R2 = ( (n[i] - n[i+1])/(n[i] + n[i+1]))^2
@lnacquaroli
lnacquaroli / blackbodyRadiation.jl
Last active September 3, 2019 02:15
Spectral radiance given the wavelength and the absolute temperature
"""
Returns the spectral radiance (W·sr^-1·m^-3), given the wavelength (m) and the absolute temperature (K).
B = blackbodyRadiation(λ, T)
B = blackbodyRadiation((.1:.01:3)*1e-6, [3000., 4000, 5000])
source: https://en.wikipedia.org/wiki/Planck%27s_law
"""
function blackbodyRadiation(λ::AbstractArray{T1}, T::AbstractArray{T2}) where {T1<:Number, T2<:Number}
h = 6.626070040e-34 # Planck's constant, J·s
c = 299792458 # speed of light, m·s
kB = 1.38064852e-23 # Boltzmann constant, J·K^-1
@lnacquaroli
lnacquaroli / wavelength2rgb.jl
Last active September 3, 2019 02:16
RGB matrix-color based on the wavelength
"""
Returns an RGB matrix-color based on the wavelength input in nm.
RBG = wavelength2rgb(λ)
Wavelength range: 380nm and 780nm, black otherwise.
Source: https://stackoverflow.com/questions/3407942/rgb-values-of-visible-spectrum
"""
function wavelength2rgb(λ::AbstractArray{<:Number})
Γ = 0.8
# warm up
R = Array{Float64, 1}(undef, length(λ))
@lnacquaroli
lnacquaroli / baselineCorrection.jl
Last active May 15, 2024 02:12
Baseline correction on data
"""
Asymmetric Least Squares Smoothing by P. Eilers and H. Boelens in 2005
For a more options and detailed implementation, see https://github.com/charlesll/Spectra.jl/blob/master/src/baseline.jl
Requires using LinearAlgebra and SparseArrays
"""
function baselineCorrection(y, lam, p, ninter=10)
L = length(y)
eyeL = Matrix(1.0I, L, L)
D = sparse(diff(diff(eyeL, dims=2), dims=2))
w = ones(L)
@lnacquaroli
lnacquaroli / movingAverage.jl
Last active September 1, 2019 02:58
Implementation of moving average of a vector x over a windows w.
"""
E.g.:
x = collect(1:100)
y = movingAverage(x, w)
"""
function movingAverage(x::Vector, wind::Int)
y = Vector{Float64}(undef, length(x))
for i in 1:length(x)
lo = max(1, i - wind)
hi = min(len, i + wind)
@lnacquaroli
lnacquaroli / findClosest.jl
Created December 11, 2018 18:34
Find the index of the closest element in an array x to a given number x0.
function findClosest(x::Array, x0::Number)
return findmin(abs.(x .- x0))[2]
end
@lnacquaroli
lnacquaroli / savitzkyGolay.jl
Last active May 18, 2021 19:40
Implementation of the Saviztky-Golay filter in julia version 1.0 or later. Adapted from: https://github.com/BBN-Q/Qlab.jl/blob/master/src/SavitskyGolay.jl
"""
Polynomial smoothing with the Savitzky Golay filters. Adapted for julia >= 1.0.
Sources: https://github.com/BBN-Q/Qlab.jl/blob/master/src/SavitskyGolay.jl
Requires LinearAlgebra and DSP modules loaded.
"""