Skip to content

Instantly share code, notes, and snippets.

@teburd
Created October 1, 2011 16:54
Show Gist options
  • Save teburd/1256317 to your computer and use it in GitHub Desktop.
Save teburd/1256317 to your computer and use it in GitHub Desktop.
Sine Wave Points
%% @doc Generate a set of points representing a sine wave
%% given some parameters (position, points to generate, freqency, amplitude,
%% offset, sample rate).
%% @end
sine(X, N=0, Frequency, Amplitude, Offset, SampleRate)
when is_integer(X), is_integer(N), is_float(Frequency),
is_float(Amplitude), is_float(Offset), is_float(SampleRate)->
[];
sine(X, N, Frequency, Amplitude, Offset, SampleRate)
when is_integer(X), is_integer(N), is_float(Frequency),
is_float(Amplitude), is_float(Offset), is_float(SampleRate) ->
Y0 = math:sin(2*math:pi()*X*(Frequency/SampleRate)),
Y1 = Amplitude*Y0,
Y2 = round(Y1),
Y3 = Y2 + Offset,
[X, Y3 | sine(X+1, N-1, Frequency, Amplitude, Offset, SampleRate)].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment