def BlackHoles(windowSize=2000, step=100): """ Task 9.5 """ file = r"sounds/BlackHolesCollision.wav" sound, fs = Sound.Read(file) Sound.Play(sound, fs) sound = sound[:, 0] # The input signal has two channels (amplitude and time) - we'll need just the amplitude N = len(sound) X, Y, Z = [], [], [] # For the contour plot i = 0 # Index of the window beginning while i + windowSize < N: window = sound[i:(i + windowSize)] ft = np.fft.fft(window) / windowSize #ft = FourierTransform(window) frequencies, amplitudes = AmplitudeSpectrum(ft, fs) X.append(np.linspace(i / fs, i / fs, windowSize // 2)) Y.append(frequencies) Z.append(amplitudes) i += step plt.contourf(np.array(X), np.array(Y), np.array(Z), cmap=cm.hot) plt.xlabel("$t$ [s]") plt.ylabel("$f$ [Hz]") plt.colorbar() plt.ylim(0, 500) plt.show()
def TestSignal(N=2000, fs=2000): """ Task 9.3 """ x = np.linspace(0, N / fs, N, endpoint=False) y = 0.1 * np.sin(440 * 2 * np.pi * x) + 0.2 * np.sin( 5 / 4 * 440 * 2 * np.pi * x) + 0.3 * np.sin( 3 / 2 * 440 * 2 * np.pi * x) Sound.Play(y, fs) ft = FourierTransform(y) plt.plot(*AmplitudeSpectrum(ft, fs)) plt.title(f"$f_s={fs}$ Hz") plt.xlabel("$f$ [Hz]") plt.ylabel("$A$") plt.show() print(y)
def FTMethodComparison(): """ Task 9.6 """ file = r"sounds/a.wav" sound, fs = Sound.Read(file) Sound.Play(sound, fs) sound = sound[3000:5000] N = len(sound) ft1 = FourierTransform(sound) plt.plot(*AmplitudeSpectrum(ft1, fs), label="FourierTransform") ft2 = np.fft.fft(sound) / N plt.plot(*AmplitudeSpectrum(ft2, fs), label="numpy.fft.fft") plt.xlabel("$f$ [Hz]") plt.ylabel("$A$") plt.legend() plt.show()
def Vowels(part): """ Task 9.4 """ path = r"sounds/" files = ["a.wav", "e.wav", "i.wav", "o.wav", "u.wav"] for file in files: sound, fs = Sound.Read(path + file) Sound.Play(sound, fs) sound = sound[part] ft = FourierTransform(sound) plt.plot(*AmplitudeSpectrum(ft, fs), label=file) plt.xlim( 0, 2000 ) # Just the lowest part of the spectrum (it contains the important frequencies) plt.xlabel("$f$ [Hz]") plt.ylabel("$A$") plt.legend() plt.show()
def main(display): #init dims = MyDims() cam = Capture.Cam() cam.scale(dims.scaledown) dims.camDims(cam.w, cam.h) #start keyboard, turret KeyboardPoller.WaitKey().thread.start() turret = Turret.Controller() turret.daemon = True turret.recenter() turret.start() #target hsvtarget = None #HSV:0-180,255,255 rgbtarget = None #for display only #motion detect mode cam.getFrame() avgframe = np.float32(cam.frame) avgtimer = threading.Event() avgstarted = False modemotion = False #sound warning warningtimer = threading.Event() warningstarted = False while (1): #capture frame,position framexy = turret.xy cam.getFrame() if display: #default display displayframe = cam.frame #track target - hsv target set if not hsvtarget == None: #10 second arm warning if not turret.armed: if not warningstarted: Timer.Countdown(10, warningtimer).thread.start() sleep(.1) #timer set warningstarted = True Sound.Play("/home/pi/robot/snd-warning.wav").thread.start() elif not warningtimer.isSet(): warningtimer.set() #once per reset Sound.Play("/home/pi/robot/snd-armed.wav").thread.start() turret.armed = True #find best contour in hsv range framehsv = cv2.cvtColor(cam.frame, cv2.COLOR_BGR2HSV) framemask = cv2.inRange(framehsv, dims.hsvlower, dims.hsvupper) if display: #color copy displaymaskframe = cv2.cvtColor(framemask.copy(), cv2.COLOR_GRAY2BGR) displayframe = cv2.addWeighted(cam.frame, 0.7, displaymaskframe, 0.3, 0) best_cnt = getBestContour(framemask, dims.areathreshold) if not best_cnt == None: #if match found #get center M = cv2.moments(best_cnt) cx, cy = int(M['m10'] / M['m00']), int(M['m01'] / M['m00']) #update turret turret.sendTarget(dims.coordToPulse((cx, cy)), framexy) if display: #display circle target objcenter = (cx, cy) objradius = int( round(math.sqrt(cv2.contourArea(best_cnt)), 2)) cv2.circle(displayframe, objcenter, objradius, (0, 0, 0), 5) cv2.circle(displayframe, objcenter, objradius, rgbtarget, 3) #wait for motion if hsvtarget == None and modemotion == True: if not avgstarted: print "accumulating average..." Timer.Countdown(5, avgtimer).thread.start() sleep(.1) #timer set avgstarted = True if avgtimer.isSet(): cv2.accumulateWeighted(cam.frame, avgframe, 0.8) resframe = cv2.convertScaleAbs(avgframe) else: print "waiting for motion..." #mask all motion motionmask = cv2.absdiff(cam.frame, resframe) motionmask = cv2.cvtColor(motionmask, cv2.COLOR_RGB2GRAY) _, motionmask = cv2.threshold(motionmask, 25, 255, cv2.THRESH_BINARY) if display: displayframe = motionmask.copy() #find best cntmask = motionmask.copy() cnt = getBestContour(cntmask, dims.areathreshold) if not cnt == None: #motion found - mask best, get mean cv2.rectangle(motionmask, tuple([0, 0]), tuple([cam.w, cam.h]), (0, 0, 0), -1) cv2.drawContours(motionmask, tuple([cnt]), 0, (255, 255, 255), -1) rgbtarget = cv2.mean(cam.frame, motionmask) framehsv = cv2.cvtColor(cam.frame, cv2.COLOR_BGR2HSV) hsvtarget = cv2.mean(framehsv, motionmask) #set target dims.setHsvRange(hsvtarget) modemotion = False if display: displayframe = cv2.resize(displayframe, (dims.displayw, dims.displayh)) cv2.imshow('display', displayframe) key = cv2.waitKey(1) #transfer char from opencv window if key > 0: KeyboardPoller.keypressed.set() KeyboardPoller.key = chr(key) #key handler if KeyboardPoller.keypressed.isSet(): if KeyboardPoller.key == "q": #quit print "Exiting..." turret.quit() cam.quit() cv2.destroyAllWindows() break elif KeyboardPoller.key == " ": #reset all print "Reset." Sound.Play("/home/pi/robot/snd-disarmed.wav").thread.start() hsvtarget = None turret.armed = False avgstarted = False warningstarted = False turret.recenter() elif KeyboardPoller.key == "1": #start motion detect print "Start motion detect." Sound.Play("/home/pi/robot/snd-motion.wav").thread.start() modemotion = True else: #manual/adjust/toggle if KeyboardPoller.key == "2": #sample center of image print "Sample center HSV." framecenter = cam.frame[(dims.h / 2) - dims.sampleradius:(dims.h / 2) + dims.sampleradius, (dims.w / 2) - dims.sampleradius:(dims.w / 2) + dims.sampleradius] framecenterhsv = cv2.cvtColor(framecenter, cv2.COLOR_BGR2HSV) hsvtarget = [ int(cv2.mean(framecenterhsv)[0]), int(cv2.mean(framecenterhsv)[1]), int(cv2.mean(framecenterhsv)[2]) ] rgbtarget = [ int(cv2.mean(framecenter)[0]), int(cv2.mean(framecenter)[1]), int(cv2.mean(framecenter)[2]) ] if KeyboardPoller.key == "p": #toggle armed turret.armed = not turret.armed if KeyboardPoller.key == "l": #target trigger sensitivity turret.firesensitivity += .01 if KeyboardPoller.key == "m": turret.firesensitivity -= .01 if KeyboardPoller.key == "a": #hue dims.hsvrange[0] += dims.adjuststep if KeyboardPoller.key == "z": dims.hsvrange[0] -= dims.adjuststep if KeyboardPoller.key == "s": #sat dims.hsvrange[1] += dims.adjuststep if KeyboardPoller.key == "x": dims.hsvrange[1] -= dims.adjuststep if KeyboardPoller.key == "d": #val dims.hsvrange[2] += dims.adjuststep if KeyboardPoller.key == "c": dims.hsvrange[2] -= dims.adjuststep if KeyboardPoller.key == "f": #area dims.areathreshold += dims.adjuststep if KeyboardPoller.key == "v": dims.areathreshold -= dims.adjuststep #adjust target settings/range if not hsvtarget == None: dims.setHsvRange(hsvtarget) print ">> HSV:", hsvtarget[0], hsvtarget[1], hsvtarget[ 2], "Range:", dims.hsvrange[0], dims.hsvrange[1], dims.hsvrange[ 2], "Area:", dims.areathreshold, "Armed:", turret.armed, turret.firesensitivity #reset key polling KeyboardPoller.WaitKey().thread.start()