Skip to content

Instantly share code, notes, and snippets.

@code-later
Created June 27, 2011 08:52
Show Gist options
  • Save code-later/1048529 to your computer and use it in GitHub Desktop.
Save code-later/1048529 to your computer and use it in GitHub Desktop.
acf, x = [], []
time_val = 0.002
i = time_val
# init function 'x[i] = A0 * cos(2*pi*f*i); f = 100Hz'
freq = 100.0 # Hz
sample_window = (freq/time_val).to_i+1
(freq*2/time_val).to_i.times do
# puts "#{i}: #{2.0*Math::PI*f*i}"
x << 1.0 * Math.cos(2.0*Math::PI*freq*i)
i += time_val
end
puts "-------"
puts "Sample Window: #{sample_window}"
puts "-------"
puts "Samples: #{x.size}"
puts "-------"
puts "Some Values of x:\n#{x[100..200].join(" | ")}"
puts "-------"
puts
# puts "Funktionswerte von: 'x[i] = A0 * cos(2*pi*f*i); f = 100Hz'"
# x.each { |y| puts y }
dec = 0
# fill the array 'r' for testing the amplitudes at specific phase
for l in 0..1000 do
acf[l] = 0.0
sample_window.times do |n|
acf[l] += x[n] * x[n+l]
# dec += x[n+l] * x[n+l]
end
acf[l] /= acf[0]
end
# puts
# puts "-------"
# r.each { |e| puts r }
# puts "-------"
# puts
n = 1
start = n
variance = 0.000001
test_val = [acf[start] + variance, acf[start] - variance]
while n < acf.size-4
n += 1
break if (acf[n] < test_val[0] && acf[n] > test_val[1])
# if (r[n] < test_val[0] && r[n] > test_val[1])
# puts n
# end
# puts n
# break if n > 500
end
periode = n-start
puts "-------"
puts "Periodendauer: #{periode}"
puts "-------"
puts "Maximum in ACF Field: #{acf.max}"
puts "-------"
puts "Size of ACF Field: #{acf.size}"
puts "-------"
puts "Erster Wert von ACF: #{acf.first}"
puts "-------"
acf.each { |e| puts e if e == 1.0 }
puts "-------"
puts "#{acf[start]}, start: #{start}"
puts "#{acf[n]}, n: #{n}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment