分析一下y=x(1:800); Y=fft(y); subplot(5,2,1); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y));
时间: 2023-11-19 09:05:35 浏览: 72
这段代码主要是对长度为800的实数序列y进行FFT变换,并对其频谱进行可视化展示。具体步骤如下:
1. y=x(1:800):从向量x中取出前800个元素作为实数序列y。
2. Y=fft(y):使用FFT算法对y进行频域变换,得到复数序列Y。
3. subplot(5,2,1):将图像窗口分为5行2列,选择第1个子图作为当前绘图的子图。
4. plot(abs(Y)):在当前子图中绘制Y的模值(即Y的幅度谱)。
5. soundsc(y,fs):将y作为音频数据,使用默认的采样率fs播放y的声音。
6. w=(0:length(y)-1)'*fs/length(y):生成一个长度为800的列向量w,其中每个元素表示对应的频率。
7. plot(w,abs(Y)):在新的子图中绘制Y的幅度谱随频率变化的曲线。
通过代码的执行结果,可以观察到y的频谱主要集中在0Hz附近,其幅度谱呈现出对称的形态。在第二个子图中,可以看到随着频率的增加,幅度谱逐渐衰减,这是由于信号的高频分量被滤波器所抑制。
相关问题
修改这段代码[x,fs]=audioread('E:/MATLAB/R2017a/bin/1.wav'); %第1个数字 %subplot(5,1,1); %plot(x(1:8000)); y=x(1:800); Y=fft(y); subplot(5,2,1); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); %第2个数字 y=x(1600:2400); Y=fft(y); subplot(5,2,2); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); %第3个数字 y=x(3200:4200); Y=fft(y); subplot(5,2,3); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); %第4个数字 y=x(4800:5600); Y=fft(y); subplot(5,2,4); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); %第5个数字 y=x(6500:7400); Y=fft(y); subplot(5,2,5); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); %第6个数字 y=x(8134:8959); Y=fft(y); subplot(5,2,6); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); %第7个数字 y=x(9768:10610); Y=fft(y); subplot(5,2,7); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); %第8个数字 y=x(11380:12210); Y=fft(y); subplot(5,2,8); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); %第9个数字 y=x(13020:13840); Y=fft(y); subplot(5,2,9); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); %第10个数字 y=x(14650:15480); Y=fft(y); subplot(5,2,10); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); 以读取和分析不同的音频文件
你可以将代码中的第一行修改为指定不同的音频文件路径:
```
[x,fs]=audioread('E:/MATLAB/R2017a/bin/1.wav'); %第1个数字
```
改为:
```
[x,fs]=audioread('E:/MATLAB/R2017a/bin/2.wav'); %第2个数字
```
以此类推,每次修改对应的数字和文件路径即可读取和分析不同的音频文件。
用for循环修改此代码[x,fs]=audioread('C:\Users\ASUS\Desktop\data信号课设\01-DTMF\1.wav'); t = linspace(0, length(x)/fs, length(x)); subplot(6,2,1); plot(t,x); title('时域图'); subplot(6,2,2); plot(x(1:800)); title('第一个分隔图'); %第1个数字 y=x(1:800); Y=fft(y); subplot(6,2,3); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); title('第一个数字'); %第2个数字 y=x(1600:2400); Y=fft(y); subplot(6,2,4); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); title('第二个数字'); %第3个数字 y=x(3200:4000); Y=fft(y); subplot(6,2,5); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); title('第三个数字'); %第4个数字 y=x(4800:5600); Y=fft(y); subplot(6,2,6); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); title('第四个数字'); %第5个数字 y=x(6400:7200); Y=fft(y); subplot(6,2,7); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); title('第五个数字'); %第6个数字 y=x(8000:8800); Y=fft(y); subplot(6,2,8); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); title('第六个数字'); %第7个数字 y=x(9600:10400); Y=fft(y); subplot(6,2,9); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); title('第七个数字'); %第8个数字 y=x(11200:12000); Y=fft(y); subplot(6,2,10); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); title('第八个数字'); %第9个数字 y=x(12800:13600); Y=fft(y); subplot(6,2,11); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); title('第九个数字'); %第10个数字 y=x(14400:15200); Y=fft(y); subplot(6,2,12); plot(abs(Y)); soundsc(y,fs); w=(0:length(y)-1)'*fs/length(y); plot(w,abs(Y)); title('第十个数字');
可以使用一个for循环来简化代码,如下所示:
[x,fs]=audioread('C:\Users\ASUS\Desktop\data信号课设\01-DTMF\1.wav');
t = linspace(0, length(x)/fs, length(x));
figure; %创建新的图形窗口
for i = 1:10 % 循环10次,每次处理一个数字
y = x((i-1)*800+1:i*800); % 从x中取出当前数字的数据
Y = fft(y); % 进行FFT变换
subplot(5,2,i); % 将图像排列在5行2列的网格中的第i个位置
plot(abs(Y)); % 绘制幅度谱
soundsc(y,fs); % 播放音频
w = (0:length(y)-1)'*fs/length(y); % 计算频率坐标
plot(w,abs(Y)); % 绘制幅度谱和频率坐标
title(['第',num2str(i),'个数字']); % 设置子图标题
end
subplot(5,2,1); title('时域图'); % 设置第一个子图的标题
阅读全文