首先输入两个音频文件(可以自己录两段音频)
[x1,fs] =audioread('voice1.wma');
[x2,fs] =audioread('voice2.wma');
fs是采样频率。
我们可以用n接收x1。再用n除以采样频率fs,即可得到x1消耗的秒数。
由于两个信号长度不同,所以接下来我们需要统一两个信号的长度
len1 = length(x1);
len2 = length(x2);
if len1>len2
x2(len2+1:len1) = 0;
else
x1(len1+1:len2) = 0;
end
derta_fs = fs/length(x1);
将较短的那个信号的空白部分用0填充。
信号等长之后直接将两个信号通入低通滤波器做成带限信号,得到信号x1_low和x2_low。
然后用audiowrite文件保存。
fp = 3000;
N1 = 2*pi*0.9/(0.1*pi);
wc1 = 2*pi*fp/fs;%将整个频率进行归一化
if rem(N1,2)
N1 = N1+1;
end
Window = blackman(N1+1);
b1 = fir1(N1,wc1/pi,Window);%低通滤波器b1只有19个数,精度不高
figure(1);
freqz(b1,1,512);
title('低通滤波器的频率响应');
x1_low =filter(b1,1,x1);%将x1低通滤波
x2_low =filter(b1,1,x2);%将x2低通滤波
audiowrite('voice1AfterLowpassFilter.wav',x1_low,fs);
audiowrite('voice2AfterLowpassFilter.wav',x2_low,fs);
注:fp=3000hz的意思是将3000hz以内的部分保留下来。
调制:用到的公式是
y(t)=x1(t)cos(wc1t)+x2(t)cos(wc2t)
fs是每秒采集的点数
而1/fs是每个点持续的秒数
所以t=i-1/fs。
x3=zeros(1,len1);
fc1 = 8000;
fc2 = 27000;%以上两句分别定义了wc1和wc2.
for i =1:length(x3)
x3(i)=x1_low(i)*cos(2*pi*fc1*(i-1)/fs)+x2_low(i)*cos(2*pi*fc2*(i-1)/fs);
end
audiowrite('voice1and2AfterModulation.wav',x3,fs);
最后解调
x1_afterModulation = zeros(1,len1);
x2_afterModulation = zeros(1,len1);
for i = 1:length(x3)
x1_afterModulation(i) = x3(i)*cos(2*pi*fc1*(i-1)/fs);
x2_afterModulation(i) = x3(i)*cos(2*pi*fc2*(i-1)/fs);
end
x1_afterModulation = filter(b1,1,x1_afterModulation);
x2_afterModulation = filter(b1,1,x2_afterModulation);
audiowrite('voice1AfterDemodulation.wav',x1_afterModulation,fs);
audiowrite('voice2AfterDemodulation.wav',x2_afterModulation,fs);
完整代码
[x1,fs] =audioread('voice1.wma');
[x2,fs] =audioread('voice2.wma');
%只取左声道
x1 = x1(:,1);
x2 = x2(:,1);
%统一两个信号的长度。
len1 = length(x1);
len2 = length(x2);
if len1>len2
x2(len2+1:len1) = 0;
else
x1(len1+1:len2) = 0;
end
derta_fs = fs/length(x1);
%低通滤波器(FIR)
fp = 3000;
N1 = 2*pi*0.9/(0.1*pi);
wc1 = 2*pi*fp/fs;
if rem(N1,2)
N1 = N1+1;
end
Window = blackman(N1+1);
b1 = fir1(N1,wc1/pi,Window);%低通滤波器b1只有19个数,精度不高
figure(1);
freqz(b1,1,512);
title('低通滤波器的频率响应');
x1_low =filter(b1,1,x1);%将x1低通滤波
x2_low =filter(b1,1,x2);%将x2低通滤波
audiowrite('voice1AfterLowpassFilter.wav',x1_low,fs);
audiowrite('voice2AfterLowpassFilter.wav',x2_low,fs);
%调制
x3=zeros(1,len1);
fc1 = 8000;
fc2 = 27000;%以上两句分别定义了wc1和wc2.
for i =1:length(x3)
x3(i)=x1_low(i)*cos(2*pi*fc1*(i-1)/fs)+x2_low(i)*cos(2*pi*fc2*(i-1)/fs);
end
audiowrite('voice1and2AfterModulation.wav',x3,fs);
%解调
x1_afterModulation = zeros(1,len1);
x2_afterModulation = zeros(1,len1);
for i = 1:length(x3)
x1_afterModulation(i) = x3(i)*cos(2*pi*fc1*(i-1)/fs);
x2_afterModulation(i) = x3(i)*cos(2*pi*fc2*(i-1)/fs);
end
x1_afterModulation = filter(b1,1,x1_afterModulation);
x2_afterModulation = filter(b1,1,x2_afterModulation);
audiowrite('voice1AfterDemodulation.wav',x1_afterModulation,fs);
audiowrite('voice2AfterDemodulation.wav',x2_afterModulation,fs);
运行结果
结果由于无法上传音频,只能给大伙看看图片。
波形明显一高一低文章来源:https://www.toymoban.com/news/detail-642240.html
再看看解调之后的文章来源地址https://www.toymoban.com/news/detail-642240.html
到了这里,关于Matlab演示低通滤波器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!