1

My goal is to compare the FFT of similar signals. For some reason, when I take the magnitude spectrum of two signals of the same length, the frequencies are different... I can't do a simple side by side comparison of two signals because of this. Anyone have any tips on how to get the same FFT on the signals?

So for instance Signal1 provides the following:

Signal 1 Signal 2

Update: Here's the two signals plotted from 0-400Hz Plotted signals

Here's my code: The logic behind the code is to import the signal, find where the sound starts, chop the signal to be 1 second in length, perform FFT on signal for comparison.

import numpy as np
from scipy.io.wavfile import read
from pylab import plot
from pylab import plot, psd, magnitude_spectrum
import matplotlib.pyplot as plt

#Hello Signal!!!
(fs, x) = read('C:\Desktop\Spectral Work\EB_AB_1_2.wav') 

#Remove silence out of beginning of signal with threshold of 1000 

def indices(a, func):
    #This allows to use the lambda function for equivalent of find() in matlab
    return [i for (i, val) in enumerate(a) if func(val)]

#Make the signal smaller so it uses less resources
x_tiny = x[0:100000]
#threshold is 1000, 0 is calling the first index greater than 1000
thresh = indices(x_tiny, lambda y: y > 1000)[1]
# backs signal up 20 bins, so to not ignore the initial pluck sound...
thresh_start = thresh-20
#starts at threshstart ends at end of signal (-1 is just a referencing thing)
analysis_signal = x[thresh_start-1:] 

#Split signal so it is 1 second long
one_sec = 1*fs
onesec = x[thresh_start-1:one_sec]

#***unsure is just a placeholder because it spits out a weird error if I don't use
#a third variable
(xsig, ysig, unsure) = magnitude_spectrum(onesec, Fs=fs)

xsig is the amplitude and ysig is the Frequencies.

Here's links to the .wav files if you're interested in trying it out yourself: .wav1 .wav2 Note: originally i uploaded the wrong .wav1 file... the correct one is now up.

8
  • Surely this breaks at thresh-20, since thresh is a list, not an np.array? Commented May 6, 2015 at 21:47
  • "unsure is just a placeholder because..." - the convention here would be to use xsig, ysig, _ = magnitude_spectrum..., ie an underscore for unwanted values Commented May 6, 2015 at 21:49
  • @Eric - it doesn't break. Though I didn't even realize that could have been a problem. And thanks for the convention tip! I'll change my code accordingly. Commented May 6, 2015 at 21:56
  • I missed the [1]. Are you sure you don't want [0], if you've come from matlab. indices(x_tiny, lambda y: y > 1000)[1] is better spelt x_tiny[x_tiny > 1000][1] Commented May 6, 2015 at 22:04
  • Or the slightly faster version, x_tiny[nonzero(x_tiny > 1000)[1]] Commented May 6, 2015 at 22:10

1 Answer 1

2

I'm guessing your signals aren't actually the same length. If you're thresholding them independently, your thresh_start value won't be the same, so:

onesec = x[thresh_start-1:one_sec]

will give you different-length arrays for the two files. You can either calculate the threshold value separately and then provide that number to this module as a constant, or make your onesec array be the same length from the start of each threshold value:

onesec = x[thresh_start-1:one_sec+thresh_start-1]

(Remember that slice notations is [start:stop], not [start:length])

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @tzaman - that's really helpful. Do you know of any sites that i can read to learn more about slice notations? I used code academy to get as far as I did... but it obviously didn't stick if they talked about it.
@ClaytonPipkin you're welcome! Just read through the official tutorial, it's pretty good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.