Comparing FFTs for Array[Double] in Scala

107 views
Skip to first unread message

Ian Downard

unread,
Apr 12, 2018, 12:56:05 AM4/12/18
to Scala Breeze
I'm trying to process a stream of vibration data using Spark. I want to detect when the vibration changes from "normal" by continuously calculating a Fourier Transform for a recent window of streamed data, then comparing that FFT with another FFT taken from vibration data captured during a period of "normal" behavior.

I'm able to use breeze to calculate an FFT for a DenseVector[Double], but I'm not sure how to do that for an Array[Double], and I don't know how to calculate the relative difference between two FFTs.  So, I have two questions:

1) How can you calculate an FFT for Array[Double] using breeze.signal.fourierTr?

2) How can I compare the results from two FFTs?

Thanks for any help.

David Hall

unread,
Apr 12, 2018, 1:08:18 AM4/12/18
to scala-...@googlegroups.com
1) You can easily make a DenseVector from an Array by doing new DenseVector(array) 

2) Do you have a particular method in mind? I don't really know what you mean, but probably the easiest thing (I guess?) is to compare the distance between the FFTs, maybe normalizing? If you're normalizing, you can compute the cosine similarity between two DenseVector[Complex] by doing (a dot b) / math.sqrt((a dot a) * (b dot b)). You might want to drop the phase from the FFT first, in which case you should just get the real component of the complex numbers.

Breeze also has built-in cosineDistance, euclideanDistance, etc., though you'll have to add an implicit UFunc implementation for complex numbers if you're not dropping phase. That looks like this:

implicit object CanNormComplex extends norm.Impl[Complex, Double] {
  def apply(x: Complex) = x.abs
}

I didn't test that, but it should work.

Hope that helps!

-- David



--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze+unsubscribe@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-breeze/a53a2ccd-764c-4d51-a763-7bf07a6b91e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ian Downard

unread,
Apr 12, 2018, 2:29:14 PM4/12/18
to Scala Breeze
This works great.  Thanks a lot!


On Wednesday, April 11, 2018 at 10:08:18 PM UTC-7, David Hall wrote:
1) You can easily make a DenseVector from an Array by doing new DenseVector(array) 

2) Do you have a particular method in mind? I don't really know what you mean, but probably the easiest thing (I guess?) is to compare the distance between the FFTs, maybe normalizing? If you're normalizing, you can compute the cosine similarity between two DenseVector[Complex] by doing (a dot b) / math.sqrt((a dot a) * (b dot b)). You might want to drop the phase from the FFT first, in which case you should just get the real component of the complex numbers.

Breeze also has built-in cosineDistance, euclideanDistance, etc., though you'll have to add an implicit UFunc implementation for complex numbers if you're not dropping phase. That looks like this:

implicit object CanNormComplex extends norm.Impl[Complex, Double] {
  def apply(x: Complex) = x.abs
}

I didn't test that, but it should work.

Hope that helps!

-- David


On Wed, Apr 11, 2018 at 11:12 AM, Ian Downard <kaya...@gmail.com> wrote:
I'm trying to process a stream of vibration data using Spark. I want to detect when the vibration changes from "normal" by continuously calculating a Fourier Transform for a recent window of streamed data, then comparing that FFT with another FFT taken from vibration data captured during a period of "normal" behavior.

I'm able to use breeze to calculate an FFT for a DenseVector[Double], but I'm not sure how to do that for an Array[Double], and I don't know how to calculate the relative difference between two FFTs.  So, I have two questions:

1) How can you calculate an FFT for Array[Double] using breeze.signal.fourierTr?

2) How can I compare the results from two FFTs?

Thanks for any help.

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages