Skip to content

Instantly share code, notes, and snippets.

@huned
Created September 13, 2012 17:30
Show Gist options
  • Select an option

  • Save huned/3716029 to your computer and use it in GitHub Desktop.

Select an option

Save huned/3716029 to your computer and use it in GitHub Desktop.
A naive downsampling implementation because I simply have too much data.
# A naive downsample implementation.
#
# arguments:
#
# * `data`: the array of data you want to sample
# * `sampleLength`: the number of samples you want
#
# returns a two element array, where the 1st
# element, k, is the sampling period (ie, sample
# every kth element) and the 2nd element is an
# array of all samples.
#
downsample: (data, sampleLength) ->
# compute k, where we want every kth value in the data array
k = Math.floor(data.length / sampleLength)
# just return the values if we want all of them
return [1, data] if k <= 1
# return an array with every nth value
[k, _.filter(data, (v, i) -> i % k == 0)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment