我是否应该担心FFT降低6倍

时间:2018-06-25 19:22:48

标签: python audio fft

当我尝试将wav文件转换为数据集(csv文件)时,一切似乎都工作正常,当我尝试对其进行FFT转换时,除了将其关闭6倍外,其他一切似乎都正常运行这是我应该担心的事情吗?

此外,我还有一个关于wav文件的问题,因此我在其中找到了数据并从中获取了整数。它们是2字节带符号的短裤,因此我知道它们的极限为-32767-32767,并且为振幅。但是,我对这些如何转换为实际体积感到困惑?

代码

import pyaudio
import wave
import time
import sys
import struct
import csv

# all files and stuffs need to contain their tags ('potatos.wav')

def wavToData(wavName):
    stream = wave.open(wavName)

    num_channels = stream.getnchannels()
    sample_rate = stream.getframerate()
    sample_width = stream.getsampwidth()
    num_frames = stream.getnframes()

    raw_data = stream.readframes( num_frames ) # Returns byte data
    stream.close()

    total_samples = num_frames * num_channels
    if sample_width == 1: 
        fmt = "%iB" % total_samples # read unsigned chars
    elif sample_width == 2:
        fmt = "%ih" % total_samples # read signed 2 byte shorts
    else:
        raise ValueError("Only supports 8 and 16 bit audio formats.")

    total_samples = num_frames * num_channels

    integer_data = struct.unpack(fmt, raw_data) # this is amplitude
    return integer_data, num_frames, sample_rate 

def dataToWav(wavName,data):
    # IMPORTANT hard coded standard values, refenced from the audiocheck.net files
    stream = wave.open(wavName,"wb")
    stream.setnchannels(1)
    stream.setframerate(44100)
    stream.setsampwidth(2)
    numFrames = len(data)
    stream.setnframes(numFrames)
    fmt = "%ih" % numFrames 

    data = struct.pack(fmt,*data) #unpacks all
    stream.writeframes(data)


def wavToCsv(wavName,csvName): 
    with open(csvName, 'wb') as csvfile: #contain all data in 1 row
                                                # x = time (1 frame)
                                                # y = freq
        filewriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)

        #get data
        integer_data, num_frames, sample_rate = wavToData(wavName)
        #adds headings and other needed data
        frameCount = [x+1 for x in range(num_frames)] # x
        fullData = [x for x in integer_data] # y
        frameCount.insert(0, "Time (increments of %f)"%(1/sample_rate))
        fullData.insert(0,"Frequency(dBfs)")
        fullData = frameCount+fullData
        filewriter.writerow(fullData)

def mergeWavData(wav1,wav2,mergeWav): #generates a brand new wav file
    firstData = wavToData(wav1)[0]
    secondData = wavToData(wav2)[0]

    limitCheck = lambda x,y: ((x+y)> 32767 or (x+y) < -32767)
    limitConstrain = lambda x,y: 32767 if (x+y) >32767 else -32767
    newData = []

    if len(firstData) < len(secondData): #adds the amplitudes of both wav file up to the shortest one
        for i in range(len(firstData)):
            data = 0
            if(limitCheck(firstData[i],secondData[i])): #exceed limit
               data = limitConstrain(firstData[i],secondData[i])
            else:
               data = firstData[i]+secondData[i]

            newData.append(data)

    else:
        for i in range(len(secondData)):
            data = 0
            if(limitCheck(firstData[i],secondData[i])): #exceed limit
               data = limitConstrain(firstData[i],secondData[i])

            else:
               data = firstData[i]+secondData[i]

               if (data > 32767 or data <-32767):
                 print data
            newData.append(data)


    dataToWav(mergeWav,newData)

mergeWavData('audiocheck.net_sin_1000Hz_-3dBFS_3s.wav','audiocheck.net_sin_4000Hz_-3dBFS_3s.wav','1000Hz+4000Hz.wav')
wavToCsv('1000Hz+4000Hz.wav','1000Hz+4000Hz.csv') # maeks csv out of wav ayayayay
#include tags (.wav/.csv)
import pyaudio
import wave
import time
import sys
import struct
import csv

# all files and stuffs need to contain their tags ('potatos.wav')

def wavToData(wavName):
    stream = wave.open(wavName)

    num_channels = stream.getnchannels()
    sample_rate = stream.getframerate()
    sample_width = stream.getsampwidth()
    num_frames = stream.getnframes()

    raw_data = stream.readframes( num_frames ) # Returns byte data
    stream.close()

    total_samples = num_frames * num_channels
    if sample_width == 1: 
        fmt = "%iB" % total_samples # read unsigned chars
    elif sample_width == 2:
        fmt = "%ih" % total_samples # read signed 2 byte shorts
    else:
        raise ValueError("Only supports 8 and 16 bit audio formats.")

    total_samples = num_frames * num_channels

    integer_data = struct.unpack(fmt, raw_data) # this is amplitude
    return integer_data, num_frames, sample_rate 

def dataToWav(wavName,data):
    # IMPORTANT hard coded standard values, refenced from the audiocheck.net files
    stream = wave.open(wavName,"wb")
    stream.setnchannels(1)
    stream.setframerate(44100)
    stream.setsampwidth(2)
    numFrames = len(data)
    stream.setnframes(numFrames)
    fmt = "%ih" % numFrames 

    data = struct.pack(fmt,*data) #unpacks all
    stream.writeframes(data)


def wavToCsv(wavName,csvName): 
    with open(csvName, 'wb') as csvfile: #contain all data in 1 row
                                                # x = time (1 frame)
                                                # y = freq
        filewriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)

        #get data
        integer_data, num_frames, sample_rate = wavToData(wavName)
        #adds headings and other needed data
        frameCount = [x+1 for x in range(num_frames)] # x
        fullData = [x for x in integer_data] # y
        frameCount.insert(0, "Time (increments of %f)"%(1/sample_rate))
        fullData.insert(0,"Frequency(dBfs)")
        fullData = frameCount+fullData
        filewriter.writerow(fullData)

def mergeWavData(wav1,wav2,mergeWav): #generates a brand new wav file
    firstData = wavToData(wav1)[0]
    secondData = wavToData(wav2)[0]

    limitCheck = lambda x,y: ((x+y)> 32767 or (x+y) < -32767)
    limitConstrain = lambda x,y: 32767 if (x+y) >32767 else -32767
    newData = []

    if len(firstData) < len(secondData): #adds the amplitudes of both wav file up to the shortest one
        for i in range(len(firstData)):
            data = 0
            if(limitCheck(firstData[i],secondData[i])): #exceed limit
               data = limitConstrain(firstData[i],secondData[i])
            else:
               data = firstData[i]+secondData[i]

            newData.append(data)

    else:
        for i in range(len(secondData)):
            data = 0
            if(limitCheck(firstData[i],secondData[i])): #exceed limit
               data = limitConstrain(firstData[i],secondData[i])

            else:
               data = firstData[i]+secondData[i]

               if (data > 32767 or data <-32767):
                 print data
            newData.append(data)


    dataToWav(mergeWav,newData)

mergeWavData('audiocheck.net_sin_1000Hz_-3dBFS_3s.wav','audiocheck.net_sin_4000Hz_-3dBFS_3s.wav','1000Hz+4000Hz.wav')
wavToCsv('1000Hz+4000Hz.wav','1000Hz+4000Hz.csv') # maeks csv out of wav ayayayay
#include tags (.wav/.csv)

0 个答案:

没有答案