【MATLAB】关于音频处理的一些常用函数总结

引言:因为智能车竞赛的信标追逐规则修改为声标,需要学会对chirp信号做相关处理,这里顺便对音频处理做一个总结,本文稍显累赘,想快速上手可直接看文末所附代码

主要函数

1.读取音频–function [y,Fs] = audioread(filename, range, datatype)

audioread可以读取的音频格式有wave、flac、MP3、MPEG-4、OGG

%    File Format      BitsPerSample  Data Type of Y     Data Range of Y
%    ----------------------------------------------------------------------
%    WAVE (.wav)            8           uint8             0 <= Y <= 255
%                          16           int16        -32768 <= Y <= 32767
%                          24           int32         -2^32 <= Y <= 2^32-1
%                          32           int32         -2^32 <= Y <= 2^32-1
%                          32           single         -1.0 <= Y <= +1.0
%    ----------------------------------------------------------------------
%    FLAC (.flac)           8           uint8             0 <= Y <= 255
%                          16           int16        -32768 <= Y <= 32767
%                          24           int32         -2^32 <= Y <= 2^32-1
%    ----------------------------------------------------------------------
%    MP3 (.mp3)            N/A          single         -1.0 <= Y <= +1.0
%    MPEG-4(.m4a,.mp4)
%    OGG (.ogg)
%    ----------------------------------------------------------------------

有两个输出参数Y和FS, Y是一个m*n的矩阵,其中m为音频采样点数,n为音频通道数,FS为采样频率

%   [Y, FS]=AUDIOREAD(FILENAME, DATATYPE) specifies the data type format of
%   Y used to represent samples read from the file.
%   If DATATYPE='double', Y contains double-precision normalized samples.
%   If DATATYPE='native', Y contains samples in the native data type
%   found in the file.  Interpretation of DATATYPE is case-insensitive and
%   partial matching is supported.
%   If omitted, DATATYPE='double'.  

2.播放音频–function sound(y,fs,bits)

sound(Y,FS); %播放对应Y和FS对应参数的音频
bits为采样位数

3.快速傅里叶变换–fft(X)

将信号从时域转换到频域,MATLAB中FFT函数的意义

项目代码

file = 'chirp_鹰短尾_ 啁啾05.mp3';   %读取文件

%音频播放
[Y,FS] = audioread(file);
[m,n] = size(Y);
disp(m);    %输出采样频率
disp(n);    %输出音频通道数
disp(FS);    %输出采样频率
disp(m/FS);    %输出素材音频时长

sound(Y,FS);    %播放对应Y和FS对应参数的音频
s1 = Y(:,1);
sound(s1,FS);   %只播放其中一个通道的音频

%%音频绘制
timeArray = (0:m-1)/FS;
timeArray1 =timeArray*1000; %放大到毫秒级
figure;plot(timeArray1,s1,'k');title('Amplitude Curve');xlabel('Time(ms)');ylabel('Amplitude');

%%绘制频率信息
s1_L = length(s1);
p = fft(s1);    %计算傅里叶变换
nUniquePts = ceil((s1_L+1)/2); 
p1 = p(1:nUniquePts);   %选择前半部,因为后半部是前半部的一个镜像
p2 = abs(p1);   %取绝对值,即幅度
p3 = p2/s1_L;   %使用点数按比例缩放,这样幅度和信号长度或者它自身的频率无关
p4 = p3.^2;     %平方得到功率
if rem(s1_L,2)  %奇数,nfft需要排除奈奎斯特点
    p4(2:end) = p4(2:end)*2;
else
    p4(2:end-1) = p4(2:end-1)*2;
end
freqArray=(0:nUniquePts-1)*(sampFreq/n);%创建频率数组
figure;plot(freqArray/1000,10*log10(p4),'k');
title('Power-Frequency Curve');
xlabel('Frequency(kHz)');
ylabel('Power(dB)');

运行结果测试

在这里插入图片描述
在这里插入图片描述

发布了15 篇原创文章 · 获赞 9 · 访问量 6911

猜你喜欢

转载自blog.csdn.net/Magician0619/article/details/104778202