Giau Diep
Giau Diep

Reputation: 13

Double check that Discrete Fourier Transform plot is right

Link to saxophone file

Link to clarinet file

I'm trying to transform a file containing a soundwave. I just want to make sure that my transform looks right because it doesn't look right to me. Hopefully my links work to obtain the files, but I will post the problem and plots I've made.

enter image description here

Code for Part B:

import numpy as np
import matplotlib.pyplot as plt
from numpy import cos, sqrt, exp, sin, pi, array, real, imag
from mpl_toolkits.mplot3d import Axes3D   # import 3D axes
from matplotlib import cm
from scipy.integrate import odeint
from scipy.fft import fft, ifft, rfft, dct, dst, idct, irfft
sax=np.loadtxt('saxophone.txt')
clar=np.loadtxt('clarinet.txt')

FT1=rfft(sax)
FT2=rfft(clar)
tsax=np.linspace(0,len(FT1)/44100,len(FT1))
tclar=np.linspace(0,len(FT2)/44100,len(FT2))
plt.plot(tsax,real(FT1))
plt.xlabel('time (s)')
plt.title('Saxophone Soundwave Discrete Fourier Transform')
plt.figure()
plt.plot(tclar,real(FT2))
plt.xlabel('time (s)')
plt.title('Clarinet Soundwave Discrete Fourier Transform')
plt.show()  

enter image description here

Code for Part A & C:

import numpy as np
import matplotlib.pyplot as plt
from numpy import cos, sqrt, exp, sin, pi, array, real, imag
from mpl_toolkits.mplot3d import Axes3D   # import 3D axes
from matplotlib import cm
from scipy.integrate import odeint
from scipy.fft import fft, ifft, rfft, dct, dst, idct, irfft
sax=np.loadtxt('saxophone.txt')
clar=np.loadtxt('clarinet.txt')

plt.figure(figsize=(12, 12))
plt.subplots_adjust(hspace=.25)
plt.subplots_adjust(wspace=1)
FT1=rfft(sax)
FT2=rfft(clar)
N=len(FT1)
tsax=np.linspace(0,len(FT1)/44100,len(FT1))
tclar=np.linspace(0,len(FT2)/44100,len(FT2))
tau=tsax\[1\]
freq=array(\[k/(2\*N\*tau) for k in range(N)\])
tsax=np.linspace(0,len(sax)/44100,len(sax))
tclar=np.linspace(0,len(clar)/44100,len(clar))
plt.subplot(221)
plt.plot(tsax, sax)
plt.xlabel('time (s)')
plt.title('Saxophone Soundwave')
plt.subplot(223)
plt.plot(tclar, clar)
plt.xlabel('time (s)')
plt.title('Clarinet Soundwave')
plt.subplot(222)
plt.plot(freq/1000, abs(real(FT1)))
plt.xlabel('Frequency (kHz)')
plt.title('Saxophone Soundwave Discrete Fourier Transform Coefficients vs Frequency')
plt.subplot(224)
plt.plot(freq/1000, abs(real(FT2)))
plt.xlabel('Frequency (kHz)')
plt.title('Clarinet Soundwave Discrete Fourier Transform Coefficients vs Frequency')
plt.show()  

enter image description here

I was expecting the FFT plot to be more spread out rather than it being so concentrated in such a small region for the Fourier Transformation. I made sure I used the right functions and that it did what it was suppose to do which seems like it did. I made sure my time step tau was right and I think it is. The sample rate is 44100Hz so tau should be 1/44100 or the first nonzero point in the time array. The code looks right yet the plot feels wrong.

Hopefully someone is able to confirm or deny my plots.

Upvotes: 0

Views: 39

Answers (0)

Related Questions