#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件        :data2array.py
@说明        :
@时间        :2021/07/07 21:25:23
@作者        :shitao.li
@版本        :1.0
'''

# -*- coding:utf-8 -*-
 
import array
import os
from matplotlib import pyplot
 
fileName = 'record_fpga.raw' # 2 channel, 16 bit per sample
file = open(fileName, 'rb')
base = 1 / (1<<15)
 
shortArray = array.array('h') # int16
size = int(os.path.getsize(fileName) / shortArray.itemsize)
tmp = size
size = int(size/256) * 256
count = int(size / 2)
shortArray.fromfile(file, size) # faster than struct.unpack
file.close()
print(shortArray)
print(size)
str = "size is %d, make int of 256 is %d, frame is %d" %(tmp, size, int(size/256))
print(str)

leftChannel = shortArray[::2]
rightChannel = shortArray[1::2]
 
def showPCM(leftChannel, rightChannel, start = 0, end = 5000):
    fig = pyplot.figure(1)
 
    pyplot.subplot(211)
    pyplot.title('pcm left channel [{0}-{1}] max[{2}]'.format(start, end, count))
    pyplot.plot(range(start, end), leftChannel[start:end])
 
    pyplot.subplot(212)
    pyplot.title('pcm right channel [{0}-{1}] max[{2}]'.format(start, end, count))
    pyplot.plot(range(start, end), rightChannel[start:end])
 
    pyplot.show()
    # fig.savefig('pcm.pdf') # save figure to pdf file
 
showPCM(leftChannel, rightChannel, 0, count)

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐