用matlab 從wav文件提取聲音數據,轉換爲數組,給C文件使用

 

clear; 
close all; 
clc;

filename = ('stereo_sinwave.wav');
filename_h = ('hexdata.h');
%To see the contents of the file you created, use the type function:

%type hexdata.txt 

% [fname,pname]=uigetfile(filename,'Open Wave File');
% file=[pname,fname];

% % % % % % % % % % % % % % % % % % % % % % % % % %
[x,fs]=audioread(filename, 'native'); % 讀入聲音文件(*.wav)
whos x
x;
%x1 = x(:,1); % 抽取第 1 聲道
%x2 = x(:,2); % 抽取第 2 聲道
a = fi(x,1);
[m,n]=size(a);
z = a.hex; % or using z = hex(a);
z = z(1:1+20,:);

h = fopen(filename_h,'w');
%Use the fprintf function to write your data to the hexdata.txt file:
for k=1:m
    fprintf(h,'0x%s, 0x%s\r\n', hex(a(k,1)),hex(a(k,2))); %dec2hex  dec 2 hex string
end
fclose(h);

% % % % % % % % % % % verify the data 
% Ways to Import Text Files
tic
filename = ('hexdata.h');
fid = fopen(filename,'r');
lines = 0;
while ~feof(fid)
     fgetl(fid);
    lines = lines +1;
end
fclose(fid);
toc
lines
% [x2, x4] = textread(filename,'0x%s 0x%s', 'delimiter', ',', ... 
%                 'emptyvalue', NaN);
[x1,x2,x3,x4] = textread(filename,'%s %s %s %s', 'delimiter', 'x ,', ... 
                'emptyvalue', NaN);  
x2;
whos x2
x = [x1,x2,x3,x4];
whos x

y1 = hex2dec(x2); %Convert hexadecimal number string to decimal number%
y2 = hex2dec(x4);
y1 = y1/(2^8);
y2 = y2/(2^8);
[m n]= size(y1)

for k=1:m
    if(y1(k)>=uint32(2^23));
        y1(k) = y1(k) - 2^24;
    end
    if(y2(k)>=uint32(2^23));
        y2(k) = y2(k) - 2^24;
    end
end 
y1 = y1/2^23;
y2 = y2/2^23;

y3 = typecast(y1, 'double');
y4 = typecast(y2, 'double');

y5 = typecast(y1, 'int32');
y6 = typecast(y2, 'int32');

y = [y1, y2];
a = fi(y3,1);

whos str1
whos str2
whos y1
whos y2
whos y3
whos y4
whos y
y55  = y(1:1+20,:)

subplot(6,1,1);
plot(y1(1:200))
subplot(6,1,4);
plot(y2(1:200))

subplot(6,1,3);
plot(y3(1:200))
subplot(6,1,2);
plot(y4(1:200))

subplot(6,1,5);
plot(y5(1:200))
subplot(6,1,6);
plot(y6(1:200))

audiowrite('33333.wav',y,48000,'BitsPerSample',24) %存儲.wav音頻文件,在這裏文件名爲sinwave.wav
info = audioinfo('33333.wav')
0x00000000, 0x7fffff00
0x10b51500, 0x7ba37500
0x2120fb00, 0x6ed9eb00
0x30fbc500, 0x5a827900
0x40000000, 0x40000000
0x4debe400, 0x2120fb00
0x5a827900, 0x00000000
0x658c9a00, 0xdedf0400
0x6ed9eb00, 0xc0000000
0x7641af00, 0xa57d8600
0x7ba37500, 0x91261400
0x7ee7aa00, 0x845c8a00
0x7fffff00, 0x80000000
0x7ee7aa00, 0x845c8a00
0x7ba37500, 0x91261400
0x7641af00, 0xa57d8600
0x6ed9eb00, 0xc0000000
0x658c9a00, 0xdedf0400
0x5a827900, 0x00000000
0x4debe400, 0x2120fb00
0x40000000, 0x40000000

 

 

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