[英]How to convert 500Hz csv data file to wav audio file?
I have a csv file of data, recorded on a data acquisition center with frequency of 500Hz, and I am trying to convert it to wav format. 我有一个csv数据文件,记录在一个频率为500Hz的数据采集中心上,我正在尝试将其转换为wav格式。 I have trie to Python and simply feed the numbers (as 16bit integers to the wave
package), and it didn't work. 我试过Python并简单地输入数字(作为wave
包的16位整数),但它不起作用。 How should I construct a wav file from simply a stream of numbers? 我应该如何仅从数字流构造一个wav文件?
I've tried the following code, which includes normalization, and I set the dtype
to be float32 so that it would use 32-bit floating-point format according to the documentation here , it just is not generating any sounds. 我尝试了以下代码,包括规范化,并将dtype
设置为float32,以便根据此处的文档使用32位浮点格式,只是不会产生任何声音。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import scipy.io.wavfile
from numpy import *
csv_array = np.loadtxt('trimmed.csv', delimiter=',', dtype=float32)
min = np.amin(csv_array)
max = np.amax(csv_array)
med = (max + min) / 2
def f(x):
return (x - med) * (1 - (-1)) / (max - min)
f = np.vectorize(f)
wav_array = f(csv_array)
scipy.io.wavfile.write('output.wav', 500, csv_array)
The problem is with your sampling rate. 问题出在您的采样率上。 Try re-sampling the data to something like 44100 Hz (see code below). 尝试将数据重新采样为44100 Hz(请参见下面的代码)。 I do not know what effects re-sampling will have on your data. 我不知道重新采样会对您的数据产生什么影响。
import numpy as np
from scipy.io import wavfile
from scipy.signal import resample
data = np.random.uniform(-1, 1, 500)
data_resampled = resample(data, 44100)
wavfile.write('output.wav', 44100, data_resampled)
Try playing around with the rate
argument in sipy.io.wavfile.write
. 尝试在sipy.io.wavfile.write
使用rate
参数。 As the rate lowers, the frequency of the sound lowers. 随着速率降低,声音的频率降低。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.