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]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章