通信人家园

 找回密码
 注册
搜索
热搜: 家园 活动
凌华科技
恒扬科技
查看: 479|回复: 0

KW软件无线电-1 [复制链接]

军衔等级:

  新兵

注册时间:
2016-12-28
发表于 2016-12-28 21:25:43 |显示全部楼层
本帖最后由 酷玩无线电 于 2016-12-28 23:25 编辑

  先从电脑声卡采集音频波形开始,程序可以采集电脑麦克风音频,然后像示波器一样显示波形:
1)采样率8000Hz;
2)使用python的科学计算库:numpy,pyqtgraph,pylab;
3)音频输入输出使用pyaudio库;


import threading
import pyaudio
import sys
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
import pylab as plt

CHUNK = 1024
PYAUDIO_FORMAT_INT16 = pyaudio.paInt16
CHANNELS = 1
SOUND_CARD_RATE = 8000
RECORD_SECONDS = 5

class SlotCommunicate(QtCore.QObject):
    drawSlot = QtCore.pyqtSignal(object)

class DisplaySignal():
    def __init__(self):
        self.window = pg.GraphicsWindow(title="Basic plotting examples")
        self.window.resize(1000, 600)
        self.window.setWindowTitle('pyqtgraph example: Plotting')

        # Enable antialiasing for prettier plots
        pg.setConfigOptions(antialias=True)
        self.spectrum_plot = self.window.addPlot(title="Updating plot")
        self.spectrum_curve = self.spectrum_plot.plot(pen='y')
        # data = np.random.normal(size=(10,1000))
        self.spectrum_plot.showGrid(x=True, y=True)

        self.spectrum_plot.setXRange(0, 1024)
        self.spectrum_plot.setYRange(-1500, 1500)


        self.audio_capture = AudioCapture(self)
        self.audio_capture.start()

        "communication between capture and display."
        self.audio_capture.c.drawSlot.connect(self.update)

    def update(self, data):
        global ptr

        self.spectrum_curve.setData(data*5)

        if ptr == 0:

            self.spectrum_plot.enableAutoRange('xy', False)  ## stop auto-scaling after the first data set is plotted
        ptr += 1
        # timer = QtCore.QTimer()
        # timer.timeout.connect(update)
        # timer.start(50)

class AudioCapture(threading.Thread):
    def __init__(self, displaySignal):
        threading.Thread.__init__(self)

        """pyaudio open the sound card"""
        self.p = pyaudio.PyAudio()

        print pyaudio.get_portaudio_version_text()

        print "(portaduio version:", pyaudio.get_portaudio_version(), ")"

        self.audio_data = 0
        self.displaySignal = displaySignal
self.c = SlotCommunicate()

        try:
            self.in_stream = self.p.open(format=PYAUDIO_FORMAT_INT16,
                channels=CHANNELS,
                rate=SOUND_CARD_RATE,
                input=True,
                frames_per_buffer=CHUNK)

            self.out_stream = self.p.open(format=PYAUDIO_FORMAT_INT16, channels=CHANNELS,
                                         rate=SOUND_CARD_RATE,
                                         output=True)

            print "open sound card succ. Sound card rate is: ", SOUND_CARD_RATE
        except:
            print "open sound card failure!"

        print SOUND_CARD_RATE / CHUNK * RECORD_SECONDS


    def run(self):
        #for i in range(0, int(SOUND_CARD_RATE / CHUNK * RECORD_SECONDS)):
        while True:
            self.audio_data = self.in_stream.read(CHUNK)
            self.audio_data = np.fromstring(self.audio_data, dtype = np.int16)

            self.c.drawSlot.emit(self.audio_data)

            #self.displaySignal.update(self.audio_data)

            #self.outStream = self.out_stream.write(sound_card_data)

            #sound_card_data = np.ndarray.tostring(sound_card_data)

        self.in_stream.stop_stream()
        self.in_stream.close()

        self.out_stream.stop_stream()
        self.out_stream.close()

        self.p.terminate()


if __name__ == '__main__':

    app = QtGui.QApplication([])
    mw = QtGui.QMainWindow()
    mw.resize(800,800)

    ptr = 0

    spectrum_plot = DisplaySignal()

    if(sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()







使用道具 举报

您需要登录后才可以回帖 登录 | 注册

Archiver|手机版|C114 ( 沪ICP备12002291号 )|联系我们 |网站地图  

GMT+8, 2017-1-20 15:48 , Processed in 0.057309 second(s), 14 queries , Gzip On.

Copyright © 1999-2016 C114 All Rights Reserved

Discuz Licensed

回顶部