-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmianban.m
254 lines (227 loc) · 7.74 KB
/
mianban.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
% 设置音频文件所在的文件夹路径
myFolder = 'E:\dsp大作业\data\面板敲击音'; % 替换为您音频文件夹的实际路径
% 检查文件夹是否确实存在
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% 获取文件夹中所有.wav文件的列表
filePattern = fullfile(myFolder, '*.wav');
wavFiles = dir(filePattern);
%设置音频文件保存路径
savepath = 'E:\dsp大作业\picture\面板敲击音';
%%
%归一化截至角频率
cut_freq = cut_freq/fs*pi;
%设计低通滤波器
[b,a] = butter(4,cut_freq,'low');
%绘制滤波器的频率响应
figure(1);
freqz(b,a);
title('滤波器的频率响应');
%%
%循环读取文件夹中的所有.wav文件,仿照上面的代码计算频谱图,并画出处理前后的频谱图,且把全部处理后的频率特性保存到一个数组中
%这里的数组是一个二维数组,第一维表示文件序号,第二维表示频率序号,数组元素表示对应的幅度值
new_freq = [];
new_Y = [];
for k = 1:length(wavFiles)
baseFileName = wavFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
%读取音频文件
[y,fs] = audioread(fullFileName);
%做傅里叶变换,注意这里的y是复数,所以要取绝对值并且只取前一半
Y = abs(fft(y));
Y = Y(1:length(Y)/2);
%滤波
y1 = filter(b,a,y);
Y1 = abs(fft(y1));
Y1 = Y1(1:length(Y1)/2);
%画出滤波后的频谱图和时域图
figure(2);
subplot(2,1,1);
%绘制滤波后的时域图
plot(y1);
title('滤波后的时域图');
xlabel('时间');
ylabel('幅度');
subplot(2,1,2);
plot(Y1);
title('滤波后的频谱图');
xlabel('频率');
ylabel('幅度');
xlim([0,20000]);
% %保存图片
saveas(gcf,fullfile(savepath,baseFileName(1:end-4)),'png');
%截断Y1到index_max处
Y1=Y1(1:index_max);
%把截断的Y1保存到new_H中
new_Y{k} = Y1;
end
%%
%对每个new_Y{i}进行三次独立的聚类,分别为count1,count2,count3类
%初始化一个二维数组,第一维表示文件序号,第二维表示聚类序号,数组元素表示对应的幅度值
new_Y11 = [];
new_Y12 = [];
new_Y13 = [];
%设置图片保存路径
savepath1 = 'E:\dsp大作业\picture\面板敲击音\聚类后的频谱图1';
savepath2 = 'E:\dsp大作业\picture\面板敲击音\聚类后的频谱图2';
savepath3 = 'E:\dsp大作业\picture\面板敲击音\聚类后的频谱图3';
for i = 1:length(new_Y)
%转置new_Y{i}
new_Y{i} = new_Y{i}';
%利用kmeans函数聚类
[idx,C] = kmeans(new_Y{i},count1);
%画出聚类后的中心点,画在一张图上
%找到count1个中心点的在频率上的索引
new_Y{i} = new_Y{i}';
% 初始化一个向量来存储每个质心最近点的索引
nearestPoints = zeros(1, count1);
% 对于每个质心,找到最近的点
for j = 1:count1
% 遍历所有点,找到值最接近质心的点
minDistance = inf;
for k = 1:length(new_Y{i})
distance = abs(new_Y{i}(k) - C(j));
if distance < minDistance
minDistance = distance;
nearestPoints(j) = k;
end
end
end
%重新绘制频谱图,在index_Y2处的值为中心点的值,其余为0
Y2 = zeros(1,length(new_Y{i}));
for j = 1:count1
Y2(nearestPoints(j)) = C(j);
end
%取出index_Y2对应的频率和幅度,保存到new_Y21中其中new_Y21{i}的前count1个元素为频率,后count1个元素为幅度
new_Y11{i} = zeros(1,2*count1);
for j = 1:count1
new_Y11{i}(j) = nearestPoints(j)*fs/length(new_Y{i});
new_Y11{i}(j+count1) = C(j);
end
%画出聚类前后的频谱图
figure(5);
subplot(2,1,1);
plot(new_Y{i});
title('聚类前的频谱图');
xlabel('频率');
ylabel('幅度');
xlim([0,20000]);
subplot(2,1,2);
plot(Y2);
title('聚类后的频谱图');
xlabel('频率');
ylabel('幅度');
xlim([0,20000]);
%保存图片
saveas(gcf,fullfile(savepath1,wavFiles(i).name(1:end-4)),'png');
end
for i = 1:length(new_Y)
%转置new_Y{i}
new_Y{i} = new_Y{i}';
%利用kmeans函数聚类
[idx,C] = kmeans(new_Y{i},count2);
%画出聚类后的中心点,画在一张图上
%找到count2个中心点的在频率上的索引
new_Y{i} = new_Y{i}';
% 初始化一个向量来存储每个质心最近点的索引
nearestPoints = zeros(1, count2);
% 对于每个质心,找到最近的点
for j = 1:count2
% 遍历所有点,找到值最接近质心的点
minDistance = inf;
for k = 1:length(new_Y{i})
distance = abs(new_Y{i}(k) - C(j));
if distance < minDistance
minDistance = distance;
nearestPoints(j) = k;
end
end
end
%重新绘制频谱图,在index_Y2处的值为中心点的值,其余为0
Y2 = zeros(1,length(new_Y{i}));
for j = 1:count2
Y2(nearestPoints(j)) = C(j);
end
%取出index_Y2对应的频率和幅度,保存到new_Y22中其中new_Y22{i}的前count2个元素为频率,后count2个元素为幅度
new_Y12{i} = zeros(1,2*count2);
for j = 1:count2
new_Y12{i}(j) = nearestPoints(j)*fs/length(new_Y{i});
new_Y12{i}(j+count2) = C(j);
end
%画出聚类前后的频谱图
figure(6);
subplot(2,1,1);
plot(new_Y{i});
title('聚类前的频谱图');
xlabel('频率');
ylabel('幅度');
xlim([0,20000]);
subplot(2,1,2);
plot(Y2);
title('聚类后的频谱图');
xlabel('频率');
ylabel('幅度');
xlim([0,20000]);
%保存图片
saveas(gcf,fullfile(savepath2,wavFiles(i).name(1:end-4)),'png');
end
for i = 1:length(new_Y)
%转置new_Y{i}
new_Y{i} = new_Y{i}';
%利用kmeans函数聚类
[idx,C] = kmeans(new_Y{i},count3);
%画出聚类后的中心点,画在一张图上
%找到count3个中心点的在频率上的索引
new_Y{i} = new_Y{i}';
% 初始化一个向量来存储每个质心最近点的索引
nearestPoints = zeros(1, count3);
% 对于每个质心,找到最近的点
for j = 1:count3
% 遍历所有点,找到值最接近质心的点
minDistance = inf;
for k = 1:length(new_Y{i})
distance = abs(new_Y{i}(k) - C(j));
if distance < minDistance
minDistance = distance;
nearestPoints(j) = k;
end
end
end
%重新绘制频谱图,在index_Y2处的值为中心点的值,其余为0
Y2 = zeros(1,length(new_Y{i}));
for j = 1:count3
Y2(nearestPoints(j)) = C(j);
end
%取出index_Y2对应的频率和幅度,保存到new_Y23中其中new_Y23{i}的前count3个元素为频率,后count3个元素为幅度
new_Y13{i} = zeros(1,2*count3);
for j = 1:count3
new_Y13{i}(j) = nearestPoints(j)*fs/length(new_Y{i});
new_Y13{i}(j+count3) = C(j);
end
%画出聚类前后的频谱图
figure(7);
subplot(2,1,1);
plot(new_Y{i});
title('聚类前的频谱图');
xlabel('频率');
ylabel('幅度');
xlim([0,20000]);
subplot(2,1,2);
plot(Y2);
title('聚类后的频谱图');
xlabel('频率');
ylabel('幅度');
xlim([0,20000]);
%保存图片
saveas(gcf,fullfile(savepath3,wavFiles(i).name(1:end-4)),'png');
end
%%
%把new_Y21,new_Y22,new_Y23添加到share.mat中
save('share.mat','new_Y11','new_Y12','new_Y13','-append');
%%
%把count1,count2,count3和cut_freq添加到share1.mat中
save('share1.mat','count1','count2','count3','cut_freq');