【发布时间】:2021-05-11 12:58:48
【问题描述】:
我正在尝试将目录中的 wav 文件转换为 16khz、2 通道和签名 int 16,然后转换为原始二进制没有标头信息,这意味着我无法使用 audiowrite。我决定为此编写这个 MATLab 脚本。它是否正确?我觉得我可能忽略了一些东西。谢谢。
clc;clear all;close all
x=1;
while(x<=5)
chr = int2str(x)
FileName1=strcat('meow/',chr) %Loading File Name
FileName=strcat(FileName1,'.wav')
%Read the data to the MATLAB using audioread.
[y,fs] = audioread(FileName);
%change to 16000 sampling rate and sign16 stream
newfile = strcat('meow/',chr);
y = int16(y);
fs2=16000;
y1=[];
y1(:,1) =y(1:fs/fs2:end,1);
y1(:,2) =y(1:fs/fs2:end,2);
fid = fopen(FileName, 'r');
data = fread(fid, [2, Inf], 'int16');
fclose(fid);
data=y1';
bit1 = int16(rem(floor((1 ./ [128, 64, 32, 16, 8, 4, 2, 1].') * double(data(1,:))), 2));
bit2 = int16(rem(floor((1 ./ [128, 64, 32, 16, 8, 4, 2, 1].') * double(data(2,:))), 2));
%https://www.mathworks.com/matlabcentral/answers/386841-how-to-convert-wav-file-into-binary
bit = [bit1 bit2];
FileNameend = strcat(newfile,'.bin');
save(FileNameend, 'bit'); % save using .bin
f = fopen(FileNameend, 'w');
fwrite(f, bit, 'double');
filename=strcat(newfile,'x.wav');
delete(filename);
x=x+1;
end
【问题讨论】:
-
你为什么认为你可能忽略了一些东西?
-
在我看来有点过于复杂了。我原以为你可以使用
audioread阅读,然后使用fwrite写作。但是,如果您所做的工作有效,那么就使用它。
标签: matlab binary signal-processing wav converters