Skip to content

Commit

Permalink
Rename harmoniccomplex to sinusoidsource (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-luke authored May 6, 2021
1 parent 4bb4eda commit 23e093b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 64 deletions.
2 changes: 1 addition & 1 deletion docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ AuditoryStimuli
```@docs
NoiseSource
CorrelatedNoiseSource
HarmonicComplex
SinusoidSource
```

## Online signal modifiers
Expand Down
5 changes: 3 additions & 2 deletions docs/src/example-hs.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Example: Harmonic Stack Complex

Harmonic stacks are often used to investigate pitch processing and streaming/grouping.
Harmonic complexes contain a set of frequency components which are harmonics.

In this example we generate a harmonic stack which is modulated at 15 Hz.

Expand All @@ -15,7 +16,7 @@ using DisplayAs # hide
# Specify the source, modifiers, and sink of our audio pipeline
stack_frequencies = 200:200:2400
source = HarmonicComplex(Float64, 48u"kHz", stack_frequencies)
source = SinusoidSource(Float64, 48u"kHz", stack_frequencies)
amp = Amplification(current=1/length(stack_frequencies),
target=1/length(stack_frequencies),
change_limit=1)
Expand Down Expand Up @@ -46,7 +47,7 @@ default(size=(700, 300)) # hide
using DisplayAs # hide
stack_frequencies = 200:200:2400
source = HarmonicComplex(Float64, 48000, stack_frequencies)
source = SinusoidSource(Float64, 48000, stack_frequencies)
am = AmplitudeModulation(15)
audio = read(source, 1.0u"s")
Expand Down
1 change: 1 addition & 0 deletions docs/src/example-itd.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ using StatsBase
lags = round.(Int, -60:1:60)
plot(lags, crosscor(sink.buf[:, 1], sink.buf[:, 2], lags),
label="", ylab="Cross Correlation", xlab="Lag (samples)")
current() |> DisplayAs.PNG # hide
```

41 changes: 0 additions & 41 deletions docs/src/offline-introduction.md

This file was deleted.

4 changes: 2 additions & 2 deletions src/AuditoryStimuli.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export bandpass_noise,
PlotSpectroTemporal,
NoiseSource,
CorrelatedNoiseSource,
HarmonicComplex,
SinusoidSource,
DummySampleSink,
Amplification,
AmplitudeModulation,
Expand All @@ -46,7 +46,7 @@ include("Plotting.jl")

include("SignalGenerators/NoiseSource.jl")
include("SignalGenerators/CorrelatedNoiseSource.jl")
include("SignalGenerators/HarmonicComplex.jl")
include("SignalGenerators/SinusoidSource.jl")
include("SignalGenerators/DummySampleSink.jl")

# #########################################
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
HarmonicComplex(eltype, samplerate, freqs)
SinusoidSource(eltype, samplerate, freqs)
HarmonicComplex is a single-channel sine-tone signal generator. `freqs` can be an
SinusoidSource is a single-channel sine-tone signal generator. `freqs` can be an
array of frequencies for a multi-frequency source, or a single frequency for a
single sinusoid source.
Expand All @@ -20,32 +20,32 @@ Output
Example
-------
```julia
source_object = HarmonicComplex(Float64, 48u"kHz", 200:200:2400)
source_object = SinusoidSource(Float64, 48u"kHz", 200:200:2400)
cn = read(source_object, 50u"ms") # Generate 50 ms of harmonic stack audio
```
"""
mutable struct HarmonicComplex{T} <: SampleSource
mutable struct SinusoidSource{T} <: SampleSource
samplerate::Float64
freqs::Vector{Float64} # in radians/sample
phases::Vector{Float64}

function HarmonicComplex(eltype, samplerate::Number, freqs::Array)
function SinusoidSource(eltype, samplerate::Number, freqs::Array)
# convert frequencies from cycles/sec to rad/sample
radfreqs = map(f->2pi*f/samplerate, freqs)
new{eltype}(Float64(samplerate), radfreqs, zeros(length(freqs)))
end
HarmonicComplex(eltype, samplerate, freq::StepRange) = HarmonicComplex(eltype, samplerate, collect(freq))
HarmonicComplex(eltype, samplerate::Number, freq::Real) = HarmonicComplex(eltype, samplerate, [freq])
HarmonicComplex(eltype, samplerate::Unitful.Frequency, freq::Real) = HarmonicComplex(eltype, samplerate |> u"Hz" |> ustrip, [freq])
HarmonicComplex(eltype, samplerate::Unitful.Frequency, freq::Array) = HarmonicComplex(eltype, samplerate |> u"Hz" |> ustrip, freq)
SinusoidSource(eltype, samplerate, freq::StepRange) = SinusoidSource(eltype, samplerate, collect(freq))
SinusoidSource(eltype, samplerate::Number, freq::Real) = SinusoidSource(eltype, samplerate, [freq])
SinusoidSource(eltype, samplerate::Unitful.Frequency, freq::Real) = SinusoidSource(eltype, samplerate |> u"Hz" |> ustrip, [freq])
SinusoidSource(eltype, samplerate::Unitful.Frequency, freq::Array) = SinusoidSource(eltype, samplerate |> u"Hz" |> ustrip, freq)
end


Base.eltype(::HarmonicComplex{T}) where T = T
nchannels(source::HarmonicComplex) = 1
samplerate(source::HarmonicComplex) = source.samplerate
Base.eltype(::SinusoidSource{T}) where T = T
nchannels(source::SinusoidSource) = 1
samplerate(source::SinusoidSource) = source.samplerate

function unsafe_read!(source::HarmonicComplex, buf::Array, frameoffset, framecount)
function unsafe_read!(source::SinusoidSource, buf::Array, frameoffset, framecount)
inc = 2pi / samplerate(source)

for i in 1:framecount
Expand Down
10 changes: 5 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,19 +336,19 @@ end
@testset "Harmonic Complex" begin

# Test instansiation
source = HarmonicComplex(Float64, 48000u"Hz", 2)
source = SinusoidSource(Float64, 48000u"Hz", 2)
@test source.samplerate == 48000
source = HarmonicComplex(Float64, 48000.7u"Hz", [100, 324, 55, 999])
source = SinusoidSource(Float64, 48000.7u"Hz", [100, 324, 55, 999])
@test source.samplerate == 48000.7
source = HarmonicComplex(Float64, 48000, 300:300:3000)
source = SinusoidSource(Float64, 48000, 300:300:3000)

source = HarmonicComplex(Float64, 48000, 2000)
source = SinusoidSource(Float64, 48000, 2000)
a = read(source, 48000)
@test size(a) == (48000, 1)


freqs = collect(200:200:2400.0)
source = HarmonicComplex(Float64, 48000, freqs)
source = SinusoidSource(Float64, 48000, freqs)
a = read(source, 48000)
b = welch_pgram(vec(a.data), fs=a.samplerate)
maxs_cart = findlocalmaxima(power(b))
Expand Down

0 comments on commit 23e093b

Please sign in to comment.