Matlab生产各种正弦波的wav文件

 本操作基于一下matlab版本

 

clear; 
close all; 
clc;

Df=1;     %频率间隔
fs = 48000; %采样频率
T = 1/fs;  %采样周期
N=fs/Df    %序列点数
time = (N-1)*T; %声音片段的总时长
t=0:T:time;
sinewave1_fs = 1000
sinewave2_fs = 2000
vol = 0; %声音片段的音量 0db
db2mag_val = (db2mag(vol))
y1 = sin(2*pi*sinewave1_fs*t)*(db2mag(vol)); %生成第一个声音片段,注意需要用db2mag()函数把dB转换成magnitude。
y2 = sin(2*pi*sinewave2_fs*t + pi/2)*(db2mag(vol));
y = [y1',y2'];
whos y
%sound(y,fs) %可以播放声音的函数 sound()
filename = ('stereo_sinwave.wav'); %给文件取名
subplot(4,1,1);
plot(t,y1())
subplot(4,1,2);
plot(t,y2)
subplot(4,1,3);
plot(y1(1:200))
subplot(4,1,4);
plot(y2(1:200))
audiowrite(filename,y,fs,'BitsPerSample',24) %存储.wav音频文件,在这里文件名为sinwave.wav
info = audioinfo(filename)
% % % % % % % % % % % % % %
[x,FS]=audioread(filename); % 将 WAV 文件转换成变量
x1 = x(:,1); % 抽取第 1 声道
x2 = x(:,2); % 抽取第 2 声道
x3 = x(1:1+20,:)

subplot(4,1,3);
plot(t,x1);
subplot(4,1,4);
plot(t,x2);

% % % % % % % % % % % % % % "mative"是要点
% % % % % % % % % % % % % % 'double'	Double-precision normalized samples.
% % % % % % % % % % % % % % 'native'	Samples in the native format found in the file.
[x,fs]=audioread(filename, 'native'); % 读入声音文件(*.wav)
whos x
x;
x1 = x(:,1); % 抽取第 1 声道
x2 = x(:,2); % 抽取第 2 声道
w1 = x1(1:2)
w2 = x2(1:2)
w = x(1:1+20,:)
% % % % % % % % % % % % % % 函数dec2hex y1必须是非负数,因此需要先负数转16进制
y1 = typecast(x1, 'uint32');
y2 = typecast(x2, 'uint32');
y = [y1, y2];
y3 = y(1:1+20,:)
% % % % % % % % % % % % % %
z1 = dec2hex(y1,8);
z2 = dec2hex(y2,8);
z = [z1, z2];
z3 = z(1:1+20,:)

z1 = [1,2,3,4]';
z2 = [11,12,13,14]';
z = [z1, z2]

猜你喜欢

转载自blog.csdn.net/qingfengjuechen/article/details/90226798
今日推荐