def showSTFT(x, samp_rate=16000): print(x) # plt.plot(range(len(x)), x) # plt.show() print (x.shape) print (type(x)) y, p = STFT.stft(x, 1024, 128) # ordinary plot print(y) # plt.imshow(y, interpolation='nearest', aspect='auto') plt.imshow(y, aspect='auto') plt.show() print(p) # plt.imshow(p, interpolation='nearest', aspect='auto') plt.imshow(p, aspect='auto') plt.show() # more sophisticated plot time = np.asarray( range(y.shape[0]), dtype="float64") * 128. / samp_rate, freq = np.asarray( range(y.shape[1]), dtype="float64") / 512. * samp_rate / 2. print(time) print(freq) ext = [0, y.shape[0] * 128. / samp_rate, 0, y.shape[1] / 512. * samp_rate / 2.] print(y) fig = plt.figure() # create a figure object ax = fig.add_subplot(1, 1, 1) # create an axes object in the figure # ax.imshow(y.T ** 0.6, aspect='auto', extent=ext, origin="lower", cmap="hot") ax.imshow(y.T ** 0.2, aspect='auto', extent=ext, origin="lower", cmap="hot") ax.set_xlabel("time [s]") ax.set_ylabel("frequency [Hz]") plt.show() print(p) fig = plt.figure() # create a figure object ax = fig.add_subplot(1, 1, 1) # create an axes object in the figure ax.imshow(p.T, aspect='auto', extent=ext, origin="lower", cmap="hot") ax.set_xlabel("time [s]") ax.set_ylabel("frequency [Hz]") plt.show()
import matplotlib.pyplot as plot import WAV as wav import STFT as stft from Oscillator import * # Load sample input... # x, sr = wav.read("wav/tomsawyer.wav") # or produce cos wave # sr = 44100.0 # osc = Oscillator(sr, 440.0) # x = 0.5 * osc.getBuffer(123456) # Set STFT parameters winSize = 1024 * 2 # in samples hopSize = winSize / 4 # in samples # Build output signal frames = stft.stft(x, winSize, hopSize) y = stft.istft(frames, winSize, hopSize) # Plot x-y difference n = min(len(x), len(y)) diff = abs(x[0:n] - y[0:n]) plot.figure() plot.plot(diff, "b") plot.show() # Write out results wav.write(x, sr, "wav/stft_input.wav") wav.write(y, sr, "wav/stft_output.wav")
import matplotlib.pyplot as plot import WAV as wav import STFT as stft from Oscillator import * # Load sample input... # x, sr = wav.read("wav/tomsawyer.wav") # or produce cos wave # sr = 44100.0 # osc = Oscillator(sr, 440.0) # x = 0.5 * osc.getBuffer(123456) # Set STFT parameters winSize = 1024*2 # in samples hopSize = winSize/4 # in samples # Build output signal frames = stft.stft(x, winSize, hopSize) y = stft.istft(frames, winSize, hopSize) # Plot x-y difference n = min(len(x), len(y)) diff = abs(x[0:n] - y[0:n]) plot.figure() plot.plot(diff, "b") plot.show() # Write out results wav.write(x, sr, "wav/stft_input.wav") wav.write(y, sr, "wav/stft_output.wav")
#============================================================================== #============================================================================== # Input signal t = arange(0, 1, 1.0 / SR) f = 1000 x = sin(2 * pi * f * t) for i in range(2, 6): x += sin(2 * pi * (f * i) * t) x = x[0:signalSize] #============================================================================== #x, SR = wav.read("wav/la.wav") #x = x[2000:5000] phasogram = stft.stft(x, windowSize, 1) phasogram = angle(phasogram) #phasogram = log(abs(phasogram)) phasogram = fft.irfft(phasogram) # Frequency bin of the input signal k = len(phasogram) * f / (SR / 2) #============================================================================== #plot.figure() #r=range(0, len(phasogram)) # display range #plot.subplot(211) #plot.title('Signalabschnitt') #plot.ylabel('[-1,1]') #plot.plot(r, x[r])
signalSize = windowSize*2 #============================================================================== #============================================================================== # Input signal t = arange(0, 1, 1.0/SR) f = 1000 x = sin(2*pi*f*t) for i in range(2,6): x += sin(2*pi*(f*i)*t) x = x[0:signalSize] #============================================================================== #x, SR = wav.read("wav/la.wav") #x = x[2000:5000] phasogram = stft.stft(x, windowSize, 1) phasogram = angle(phasogram) #phasogram = log(abs(phasogram)) phasogram = fft.irfft(phasogram) # Frequency bin of the input signal k = len(phasogram) * f / (SR/2) #============================================================================== #plot.figure() #r=range(0, len(phasogram)) # display range #plot.subplot(211) #plot.title('Signalabschnitt') #plot.ylabel('[-1,1]') #plot.plot(r, x[r])
SR = 44100 windowSize = 512 hopSize = windowSize / 4 signalSize = windowSize*5 #============================================================================== #============================================================================== # Input signal t = arange(0, 1, 1.0/SR) f = 5000 x = sin(2*pi*f*t) x = x[0:signalSize] #============================================================================== #============================================================================== frames = stft.stft(x, windowSize, hopSize) phases = zeros(len(frames[0])) tau = 1 for n in range(0, len(frames)): frameIn = frames[n] frameOut = zeros(len(frameIn),dtype=complex) maxA = (len(frameIn)-1)/2 for a in range(0, maxA): b = a * 2 + 0.5; phases[a] += (2*pi*(b-a)/windowSize)*hopSize frameOut[b] = frameIn[a] * exp(1j*phases[a])