-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Frequency module #51
Comments
I think my preference would be for a generic -- Most generic
type alias Frequency units =
Quantity Float (Rate units seconds)
-- Simpler but less flexible
type alias Frequency =
Quantity Float (Rate Unitless Seconds)
-- Simplest (and best error messages) but least flexible
type alias Frequency=
Quantity Float Hertz Can you think of what operations you might want to do on a For example, is it actually useful to multiply frequency by time to get number of occurrences? What would that look like? If it's not useful (and the only useful operations are just on frequency values themselves, like subtracting two frequencies to get a beat frequency) then |
If someone is creating a music theory package then they might want to take a frequency and raise it an octave. Or finding harmonics for a frequency. For elm-audio, maybe I could add a function that takes the sample rate of the user's audio system (currently I represent sample rate as just an int but maybe that's also a frequency?) and computes highest possible frequency supported when playing a sine wave. |
Those all sound like things that don't involve changing units - e.g. raising an octave would be just type Hertz
= Hertz
type alias Frequency =
Quantity Float Hertz instead of the more general but confusing type alias Frequency events =
Quantity Float (Rate events Seconds) I guess a compromise might be type Events
= Events
type alias Frequency =
Quantity Float (Rate Events Seconds) which opens the door to some rate-related math but avoids needing a type variable and is somewhat self-documenting. You could even have something like module Frequency exposing
( Frequency
, Events
, events
, eventCount
, hertz
, inHertz
)
type Events
= Events
type alias Frequency =
Quantity Float (Rate Events Seconds)
events : Float -> Quantity Float Events
events numEvents =
Quantity numEvents
eventCount : Quantity Float Events -> Float
eventCount (Quantity numEvents) =
numEvents
hertz : Float -> Frequency
hertz numHertz =
Quantity numHertz
inHertz : Frequency -> Hertz
inHertz (Quantity numHertz) =
numHertz which would allow for things like heartRate =
Frequency.events 55 |> Quantity.per Duration.minute
numHeartbeats =
Duration.seconds 10
|> Quantity.at heartRate
|> Frequency.eventCount (I'm not sure I'm wild about the |
To me it feels like the problem with Frequency represented in these ways is that it's too general and too similar to So the advantage with having Frequency only refer to acoustic waves is that it's clear when it use it and what it means (maybe acoustics is too specific and it could be a module for anything dealing with sine waves such as light wave frequencies).
Yeah, I think so. As mentioned, this is really not my area of expertise 😅 Edit:
That comes back to the original problem I'm having which is having a common unit both elm-audio and some elm-music-theory package can both use to represent frequencies. If I need to define what |
I guess what I like about having a built-in What do you think of the plain type Hertz
= Hertz
type alias Frequency =
Quantity Float Hertz idea? I think it addresses most use cases I can think of, is pretty simple and avoids weirdness/confusion. And for stuff like the heart rate example I came up with, it probably makes more sense to just have (in app code) something like type Heartbeats
= Heartbeats
heartbeats : Float -> Quantity Float Heartbeats
inHeartbeats : Quantity Float Heartbeats -> Float
heartRate =
heartbeats 55 |> Quantity.per Duration.minute
numHeartbeats =
Duration.seconds 10
|> Quantity.at heartRate
|> inHeartbeats |
I checked the definition of hertz and I think part of my argument that it's too similar to Rate was caused by thinking hertz represented a generic "events over time" unit. Wikipedia defines it as type alias Frequency =
Quantity Float Hertz So this works for me. |
It occurred to me that you could add a couple functions that would help compensate for -- Count number of cycles in a particular time period,
-- similar to Quantity.for
Frequency.for : Duration -> Frequency -> Float
-- Get the cycle period of a particular frequency
Frequency.period : Frequency -> Duration Then you could do things like numCycles : Float
numCycles =
Frequency.kilohertz 3.5 |> Frequency.for (Duration.milliseconds 10) to get a plain frequency : Frequency
frequency =
Frequency.hertz 55
heartRate : Quantity Float (Rate Heartbeats Seconds)
heartRate =
heartbeats 1 |> Quantity.per (Frequency.period frequency) to convert a Frequency.perCycle :
Quantity Float units
-> Frequency
-> Quantity Float (Rate units Seconds) which you could use as heartRate : Quantity Float (Rate Heartbeats Seconds)
heartRate =
Frequency.hertz 55 |> Frequency.perCycle (heartbeats 1) |
|
Yeah, |
I'm working on adding the ability to play sine/square/sawtooth/triangle waves to
elm-audio
. Currentlyelm-audio
has it's owntype alias Frequency = Quantity Float (Rate Cycles Seconds)
and acyclesPerSecond : Float -> Frequency
helper function. I started adding the ability to create frequencies from music notes when I realized that's a broad topic and not something I know enough about to properly implement. It would be useful then if bothelm-audio
and a potential futureelm-music-theory
elm package could share the same Frequency type so they don't need to have dependencies on each other.The module could look something like this:
An alternate name for this module could be
Pitch
but it seems like there is a distinction between pitch and frequency where frequency is oscillations per second and pitch is the human perception of frequencies (at least, this is what wikipedia tells me https://en.wikipedia.org/wiki/Pitch_%28music%29).Lastly, this issue is sort of related to this discussion but that discussed a generic frequency type and this is for a sound based frequency module.
The text was updated successfully, but these errors were encountered: