Exemple #1
0
def _test_fft(size):
    input = _random_vector(size)
    refout = _naive_dft(input, False)
    actualout = fft.transform(input, False)
    err = _log10_rms_err(refout, actualout)

    actualin = fft.transform(refout, True)
    actualin = [(x / size) for x in actualin]
    err = max(_log10_rms_err(input, actualin), err)
    print("fftsize={:4d}  logerr={:5.1f}".format(size, err))
def _test_fft(size):
	input = _random_vector(size)
	refout = _naive_dft(input, False)
	actualout = fft.transform(input, False)
	err = _log10_rms_err(refout, actualout)
	
	actualin = fft.transform(refout, True)
	actualin = [(x / size) for x in actualin]
	err = max(_log10_rms_err(input, actualin), err)
	print("fftsize={:4d}  logerr={:5.1f}".format(size, err))
def regressionCoefficients(wav_data, thres):
    # smoothen the signal
    wav1 = smoothing(wav_data)

    L1 = len(wav1)

    ff1 = linspace(0, 8000, L1)
    f1 = linspace(0, 4000, L1 // 2)

    xx1 = transform(wav1,False)
    xx1 = np.abs(real(xx1)) // L1

    x1 = 2 * xx1[0:L1 // 2]

    max1 = max(x1)

    # x1 = 2 * xx1[0:L1 // 2]
    # max1 = max(x1)
    x1 = x1 / max1

    # find peaks above threshold of 20%
    peaks1 = findpeaks(x1, 50, thres)
    plt.plot(f1, x1)

    # plot peaks
    plt.plot(f1[peaks1], x1[peaks1], 'ro')
    plt.show()
    freq1 = f1[peaks1]

    # Regression Coefficients
    coef1 = x1[peaks1] * max1

    return coef1
def inverse_transform(vector):
    n = len(vector)
    factor = -1j * cmath.pi / (len(vector) * 2)
    temp = [(val if i > 0 else val / 2.0) * cmath.exp(i * factor)
            for (i, val) in enumerate(vector)]
    temp = fft.transform(temp, False)

    temp = [val.real for val in temp]
    result = [None] * n
    result[::2] = temp[:(n + 1) // 2]
    result[-1 - len(vector) % 2::-2] = temp[(n + 1) // 2:]
    return result
def inverse_transform(vector):
	n = len(vector)
	factor = -1j * cmath.pi / (len(vector) * 2)
	temp = [(val if i > 0 else val / 2.0) * cmath.exp(i * factor)
		for (i, val) in enumerate(vector)]
	temp = fft.transform(temp, False)
	
	temp = [val.real for val in temp]
	result = [None] * n
	result[ : : 2] = temp[ : (n + 1) // 2]
	result[-1 - len(vector) % 2 : : -2] = temp[(n + 1) // 2 : ]
	return result
Exemple #6
0
    def update_fftplot(self):
        """Continually plots the FFT (absolute value of the complex output) of
        the EEG graph. Takes half the FFT plot due to symmetry and the Nyquist
        sampling limit.
        """
        self.fft_plot_ref = None

        while self._run_thread:
            self.fft = my_fft.transform(np.asarray(self.ydata), False)
            if self.fft_plot_ref is not None:
                self.fft_plot_ref.remove()
            self.fft_plot_ref, = self.fft_canvas.axes.plot(
                self.bins[:(self.n_data >> 1)],
                np.abs(self.fft[:(self.n_data >> 1)]), '#00FF7F', linewidth=1)

            self.fft_canvas.draw()
            # TODO: slider of the fft frequency
            time.sleep(0.1)
Exemple #7
0
def shiftallpeaks(peaks1, peaks2, wav_data3):
    """ Shift all peaks paramaters(peaks1,peaks2,wav_data3)
    """

    n = len(peaks1)
    x3 = transform(wav_data3,False)
    new = zeros(len(x3) // 2)
    new = new.astype(dtype=np.complex)

    Peaks1 = zeros(n + 2)
    Peaks1[1:n + 1] = peaks1
    Peaks1[n + 1] = len(x3) // 2
    Peaks1 = Peaks1.astype(np.int16)

    Peaks2 = zeros(n + 2)
    Peaks2[1:n + 1] = peaks2
    Peaks2[n + 1] = len(x3) // 2

    Peaks2 = Peaks2.astype(np.int16)

    for j in range(0, n + 1):
        k = 0

        for i in range(Peaks2[j], Peaks2[j + 1]):
            index = Peaks1[j] + np.floor(scale[j + 1] * k)
            k += 1
            index = int(index)
            new[i] = x3[index]

        # run this if you wish to replace duplicates with zeros

    for j in range(len(new) - 1):
        if new[j] == new[j + 1]:
            new[j] = 0.0

    return new
# plot best fit line to coefficients
xaxis = linspace(0, C1[-1], 1000)
yaxis = linspace(0, 0, 1000)

for i in range(0, 1000):
    for j in range(n):
        yaxis[i] += coef[j] * xaxis[i] ** (n - j - 1)

plt.plot(X, Y, 'ro')
plt.plot(xaxis, yaxis)

# find FFT of third audio sample
L3 = len(wav_data3)
ff3 = linspace(0, 8000, L3)
f3 = linspace(0, 4000, L3 // 2)
xx3 = transform(wav_data3,False)
xx3 = np.abs(real(xx3)) // L3
x3 = 2 * xx3[0:L3 // 2]

# push coefficients through regression model found from vector "coef"
n = len(coef)
ynew = linspace(0, 0, L3 // 2)
for i in range(L3 // 2):
    for j in range(n):
        ynew[i] += coef[j] * x3[i] ** (n - j - 1)

# plot the transformation of the coefficients
plt.plot(f3, yaxis)
plt.show()
def transform(vector):
    temp = vector[::2] + vector[-1 - len(vector) % 2::-2]
    temp = fft.transform(temp, False)
    factor = -1j * cmath.pi / (len(vector) * 2)
    return [(val * cmath.exp(i * factor)).real for (i, val) in enumerate(temp)]
Exemple #10
0
def _test_fft(size):
    input = _random_vector(size)
    refout = _naive_dft(input)
    actualout = fft.transform(input)
    print("fftsize={:4d}  logerr={:5.1f}".format(
        size, _log10_rms_err(refout, actualout)))
Exemple #11
0
def _test_fft(size):
    input = _random_vector(size)
    refout = _naive_dft(input)
    actualout = fft.transform(input)
    print("fftsize={:4d}  logerr={:5.1f}".format(size, _log10_rms_err(refout, actualout)))
Exemple #12
0
import fft
import complexity

HARMONIC_NUM = 10
FREQUENCY_LIMIT = 2700
TIME_INTERVALS_NUM = 256
COMPARISON_INTERVALS_NUM = 2 ** 10
COMPLEXITY_INTERVALS_NUM = 2 ** 15

plt.style.use("dark_background")

fig, (ax1, ax2, ax3) = plt.subplots(3, 1)
f = np.linspace(0, FREQUENCY_LIMIT, TIME_INTERVALS_NUM)
signal = signal_generator.generate(HARMONIC_NUM, FREQUENCY_LIMIT, TIME_INTERVALS_NUM)
spectrumNumPy = abs(np.fft.fft(signal))
spectrumFFT = abs(fft.transform(signal))
ax1.plot(f, spectrumNumPy, color="blue")
ax1.set_title("numpy")
ax2.plot(f, spectrumFFT, color="blue")
ax2.set_title("my fft")
counts = [1 << i for i in range(1, int(np.log2(COMPLEXITY_INTERVALS_NUM))+1)]
time = complexity.comparativeBenchmark(COMPLEXITY_INTERVALS_NUM)(fft.transform)
ax3.plot(counts, time, color="green", marker=".")
ax3.set_title("Algorithm complexity")
ax3.set_xlabel("Number of discrete intervals in signal")
ax3.set_ylabel("Time")


fig2, ax4 = plt.subplots()
benchmark = complexity.comparativeBenchmark(COMPARISON_INTERVALS_NUM)
counts = [1 << i for i in range(1, int(np.log2(COMPARISON_INTERVALS_NUM))+1)]
Exemple #13
0
FILENAME1 = "source.wav"
FILENAME2 = "target.wav"
FILENAME3 = "thirdsample.wav"
rate1, wav_data1 = wavfile.read(FILENAME1)
rate2, wav_data2 = wavfile.read(FILENAME2)  
rate3, wav_data3 = wavfile.read(FILENAME3)


wav1 = smoothing(wav_data1)
wav2 = smoothing(wav_data2)
wav3 = smoothing(wav_data3)


L1 = len(wav1)
f1 = linspace(0, 8000, L1)
x1 = transform(wav1,False)
# x1 = np.fft.fft(wav1)
X1 = np.abs(x1) / L1  # magnitude of coefficient of FFT of source voice
L2 = len(wav2)
f2 = linspace(0, 8000, L2)
# x2 = np.fft.fft(wav2)
x2 = transform(wav2,False)
X2 = np.abs(x2) / L2 
 # magnitude of  coefficient of FFT of target voice to be replicated
ratio = abs(x2) / abs(x1)  # ratio of coefficients r=d/c
Freq = linspace(0, 8000, L1)
plt.plot(Freq, ratio)  # plot ratio over double sided FFT spectrum

# This will help us cut down on the ratio .
# You need to pick values of h>1.
# The closer to 1 , the more it shrinks
def transform(vector):
	temp = vector[ : : 2] + vector[-1 - len(vector) % 2 : : -2]
	temp = fft.transform(temp, False)
	factor = -1j * cmath.pi / (len(vector) * 2)
	return [(val * cmath.exp(i * factor)).real for (i, val) in enumerate(temp)]