diff options
Diffstat (limited to 'Gen/GenTrig.swift')
-rw-r--r-- | Gen/GenTrig.swift | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/Gen/GenTrig.swift b/Gen/GenTrig.swift index 84bf9cf..cc4abb5 100644 --- a/Gen/GenTrig.swift +++ b/Gen/GenTrig.swift @@ -5,6 +5,8 @@ // Created by Jacky Jack on 26/10/2024. // +import Foundation + /// Generate sin function as float array /// - Parameters: /// - sample_rate: sample rate, how many samples per second @@ -14,8 +16,19 @@ /// - sample_num: number of samples to generate /// - Returns: Array of floats within range [-ampl ... apml] /// -func genSin(_ sample_rate: Int,_ freq: Float,_ ampl: Float,_ phase: Float,_ sample_num: Float) -> Array<Float> { - return [0.0] +func genSin(_ sample_rate: Int,_ freq: Int,_ ampl: Float,_ phase: Float,_ sample_num: Int) -> Array<Float32> { + + //let one_sec = sample_num/sample_rate + let sample_per_hz:Float32 = Float32(sample_rate)/Float32(freq) + let hz_step: Float32 = 2*Float32.pi/sample_per_hz + var out_signal:[Float32] = Array(repeating: Float(0.0), count: sample_num) + var start = phase + for i in 0..<sample_num { + out_signal[i] = ampl * sin(start) + start += hz_step + } + + return out_signal } /// Generate Cos function as float array @@ -27,7 +40,17 @@ func genSin(_ sample_rate: Int,_ freq: Float,_ ampl: Float,_ phase: Float,_ samp /// - sample_num: number of samples to generate /// - Returns: Array of floats within range [-ampl ... apml] /// -func genCos(_ sample_rate: Int,_ freq: Float,_ ampl: Float,_ phase: Float,_ sample_num: Float) -> Array<Float> { - return [0.0] +func genCos(_ sample_rate: Int,_ freq: Float,_ ampl: Float,_ phase: Float,_ sample_num: Int) -> Array<Float32> { + //let one_sec = sample_num/sample_rate + let sample_per_hz:Float32 = Float32(sample_rate)/Float32(freq) + let hz_step: Float32 = 2*Float32.pi/sample_per_hz + var out_signal:[Float32] = Array(repeating: Float32(0.0), count: sample_num) + var start = phase + for i in 0...sample_num { + out_signal[i] = ampl * cos(start) + start += hz_step + } + + return out_signal } |