用matlab 从wav文件提取声音数据,转换为数组,给C文件使用

本文介绍使用MATLAB进行音频文件读取、十六进制数据转换及验证的方法。通过实例展示如何将音频数据转换为十六进制格式并保存至文件,再从文件中读取并转换回音频格式,最后比较转换前后音频的一致性。

 

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值