K-means算法和矢量量化

语音信号的数字处理课程作业——矢量量化。这里采用了K-means算法,即假设量化种类是已知的,当然也可以采用LBG算法等,不过K-means比较简单。矢量是二维的,可以在平面上清楚的表示出来。

1. 算法描述

本次实验选择了K-means算法对数据进行矢量量化。算法主要包括以下几个步骤

  • 初始化:载入训练数据,确定初始码本中心(4个);
  • 最近邻分类:对训练数据计算距离(此处采用欧式距离),按照距离最小分类;
  • 码本更新:重新生成包腔对应的质心;
  • 重复分类和码本更新步骤,知道达到最大迭代次数或满足一定停止准则;
  • 利用上述步骤得到的码本对测试数据进行矢量量化,并求最小均方误差。

本实验准备使用MATLAB软件完成矢量量化任务,具体步骤实现如下

    1. 将training.dat和to_be_quantized.dat置于当前工作文件夹内,采用load命令载入training.dat 。
    2. 采用合适的规则选取初始的码本中心。如图 1所示。

image

图 1 码本中心选择

  1. 计算训练数据和每一码本中心之间的距离。
  2. 采用最近邻准则进行分类。
  3. 重新计算质心,计算公式如下所示。
  4. 重复3~5,直到满足最大迭代次数或是两次迭代结果没有发生改变时,此时结果为训练结果。
  5. 利用训练结果对to_be_quantized.dat进行矢量量化。

2. 代码

    MATLAB代码如下

 1 %% training
 2 load('training.dat');
 3 scatter(training(:,1),training(:,2));
 4 %初始中心选取
 5 x_max = max(training(:,1));
 6 x_min = min(training(:,1));
 7 y_max = max(training(:,2));
 8 y_min = min(training(:,2));
 9 z1 = [(3*x_min+x_max)/4 (3*y_min+y_max)/4];
10 z2 = [(3*x_max+x_min)/4 (3*y_min+y_max)/4];
11 z3 = [(3*x_min+x_max)/4 (3*y_max+y_min)/4];
12 z4 = [(3*x_max+x_min)/4 (3*y_max+y_min)/4];
13 z = [z1;z2;z3;z4];
14 hold on;
15 scatter(z(:,1),z(:,2));
16 legend('训练数据','码本');grid on;
17 hold off;
18 for k = 1:20
19     %码本分类,欧式距离
20     distancetoz1 = (training - repmat(z1,size(training,1),1)).^2;
21     distancetoz1 = sum(distancetoz1,2);
22     distancetoz2 = (training - repmat(z2,size(training,1),1)).^2;
23     distancetoz2 = sum(distancetoz2,2);
24     distancetoz3 = (training - repmat(z3,size(training,1),1)).^2;
25     distancetoz3 = sum(distancetoz3,2);
26     distancetoz4 = (training - repmat(z4,size(training,1),1)).^2;
27     distancetoz4 = sum(distancetoz4,2);
28     distance = [distancetoz1 distancetoz2 distancetoz3 distancetoz4];
29     % 分类
30     if(classification == (distance == repmat(min(distance,[],2),1,4)))
31         error = mean(min(distance,[],2));
32         break;      %如果两次迭代之间没有变化,结束迭代
33     end;
34     classification = (distance == repmat(min(distance,[],2),1,4));
35     c1 = training(classification(:,1),:);
36     c2 = training(classification(:,2),:);
37     c3 = training(classification(:,3),:);
38     c4 = training(classification(:,4),:);
39     figure;scatter(c1(:,1),c1(:,2));hold on;scatter(c2(:,1),c2(:,2));
40     scatter(c3(:,1),c3(:,2));scatter(c4(:,1),c4(:,2));
41     legend('类型1','类型2','类型3','类型4');grid on;hold off;
42     % 码本更新
43     z1 = mean(c1);
44     z2 = mean(c2);
45     z3 = mean(c3);
46     z4 = mean(c4);
47     z = [z1;z2;z3;z4];
48 end
49 %% Test
50 load('to_be_quantized.dat')
51 distancetoz1 = (to_be_quantized - repmat(z1,size(to_be_quantized,1),1)).^2;
52 distancetoz1 = sum(distancetoz1,2);
53 distancetoz2 = (to_be_quantized - repmat(z2,size(to_be_quantized,1),1)).^2;
54 distancetoz2 = sum(distancetoz2,2);
55 distancetoz3 = (to_be_quantized - repmat(z3,size(to_be_quantized,1),1)).^2;
56 distancetoz3 = sum(distancetoz3,2);
57 distancetoz4 = (to_be_quantized - repmat(z4,size(to_be_quantized,1),1)).^2;
58 distancetoz4 = sum(distancetoz4,2);
59 distance = [distancetoz1 distancetoz2 distancetoz3 distancetoz4];
60 testerror = mean(min(distance,[],2));
61 
62 classification = (distance == repmat(min(distance,[],2),1,4));
63 c1 = to_be_quantized(classification(:,1),:);
64 c2 = to_be_quantized(classification(:,2),:);
65 c3 = to_be_quantized(classification(:,3),:);
66 c4 = to_be_quantized(classification(:,4),:);
67 figure;scatter(c1(:,1),c1(:,2));hold on;scatter(c2(:,1),c2(:,2));
68 scatter(c3(:,1),c3(:,2));scatter(c4(:,1),c4(:,2));
69 legend('类型1','类型2','类型3','类型4');grid on;hold off;

3. 实验结果

image

图 2 训练码本分布

imageimage

图 3第一次迭代结果                   图 4第四次迭代结果

imageimage

图 5第八次迭代结果                  图 6第九次迭代结果

 

        图 2展示了训练数据的分布,图 3~6是迭代过程中分类的变化情况,迭代完成后的码本为

  • Z1 = [1.62060631541935 -0.108624145483871]
  • Z2 = [7.96065094375000 -0.999061308437500]
  • Z3 = [1.72161941468750 6.82121444062500]
  • Z4 = [4.43652765757576 2.18874305151515]

4. 实验数据

   training.dat

  1   8.4416189e+000 -7.9885975e-001
  2   1.1480908e+000  7.8735044e+000
  3   7.7380144e+000 -1.2165061e+000
  4   8.9727144e-001  7.3962468e+000
  5   7.5343823e+000 -1.1424504e+000
  6  -6.9234039e-001 -1.7096610e+000
  7   7.6418740e+000 -1.3563792e+000
  8   3.1091418e+000  6.3850541e+000
  9   2.3482174e+000  4.7553506e-001
 10  -1.3840364e+000 -2.5480394e+000
 11   8.2008897e+000 -1.1448387e+000
 12  -1.1392497e+000 -2.0809884e+000
 13   3.7970116e+000  1.6906469e+000
 14   3.4484200e+000  1.3980911e+000
 15   2.5701485e+000  5.3755044e+000
 16   8.3899076e+000 -6.6675309e-001
 17   2.0146545e+000  5.6984592e+000
 18   1.8853328e+000  5.2762628e-001
 19   5.6781432e+000  3.2588691e+000
 20   1.0102480e+000  5.8167707e+000
 21   7.7302763e+000 -1.2030348e+000
 22   4.2118845e+000  1.6527181e+000
 23   4.3920049e-001  6.7168970e+000
 24   8.1934984e-001 -5.1917945e-001
 25   4.3708769e+000  2.1613573e+000
 26   1.8569681e+000  4.8380565e+000
 27   3.4732504e+000  1.7953635e+000
 28   7.5822756e+000 -1.1521814e+000
 29   2.6434078e+000  6.3295690e+000
 30   1.9968582e+000  7.3529314e+000
 31   4.0833513e+000  1.4936002e+000
 32   3.6767894e+000  6.7446912e+000
 33   1.3524515e+000  6.8177858e+000
 34   3.9711504e+000  1.5452503e+000
 35   1.5594711e+000  6.3885281e+000
 36   3.4692089e+000  1.7118124e+000
 37   5.2575491e+000  2.5601553e+000
 38   7.8827882e+000 -6.8867840e-001
 39   4.8176593e+000  2.1684005e+000
 40   2.7402486e+000  8.3320174e+000
 41   2.2549011e+000  3.9393641e-001
 42   8.0840542e+000 -7.3155184e-001
 43   8.8753667e-001  6.1607892e+000
 44   1.8067727e+000 -2.1099454e-001
 45   6.8650914e+000  4.4228389e+000
 46   6.4174056e+000  3.7590081e+000
 47   4.0933273e+000  1.3598676e+000
 48   2.2882999e+000  5.1876795e-001
 49   7.9225523e+000 -1.1725456e+000
 50   4.3561335e+000  1.8976163e+000
 51   8.3279098e+000 -1.0232899e+000
 52   6.2551331e+000  3.3449949e+000
 53   3.1276024e+000  7.8463356e-001
 54   6.5241605e+000  3.4561490e+000
 55   4.1588140e-001  6.4974858e+000
 56   2.7379263e+000  6.4746080e+000
 57   7.2185639e+000 -1.3525589e+000
 58   7.5424890e+000 -1.5317814e+000
 59   3.7468423e+000  1.6110753e+000
 60   8.8708536e+000 -5.6439331e-001
 61   7.6960713e+000 -1.1960633e+000
 62   7.5979552e+000 -1.1469059e+000
 63   2.8220978e+000  1.0360184e+000
 64   3.8165165e+000  1.6082223e+000
 65   6.6799248e-002 -1.2910367e+000
 66   2.3054028e+000  2.8450986e-001
 67   4.2788715e+000  5.1995858e+000
 68   3.0006534e+000  9.1250414e-001
 69   7.6051326e+000 -1.1005476e+000
 70   2.5331653e+000  9.7428007e-001
 71   1.0743104e+000  6.0859296e+000
 72   6.7237149e-001  8.6117274e+000
 73   2.4333003e+000  7.1421389e-001
 74   1.7723473e+000  7.1841833e+000
 75   3.5762796e+000  1.5348648e+000
 76   2.7863558e+000  7.3565043e-001
 77   8.0284284e+000 -7.9636983e-001
 78   8.4672682e+000 -8.2062254e-001
 79   2.3519727e+000  8.1632796e-001
 80   7.4240720e+000  4.1800229e+000
 81   1.9724319e+000  4.4328699e-001
 82   7.7622621e+000 -1.3506605e+000
 83   2.3793018e+000 -4.3107386e-001
 84   3.2455220e+000  1.2697488e+000
 85   1.3644859e+000  5.9712644e+000
 86   5.4815655e+000  2.6608754e+000
 87  -1.2002073e+000 -2.1765731e+000
 88  -3.5558595e-001  6.4387512e+000
 89   3.9418185e+000  1.9858047e+000
 90   1.0533626e+000 -7.9068285e-001
 91   1.9560213e+000  6.2001316e+000
 92   7.5555203e+000 -1.2087337e+000
 93   1.7851705e+000  7.0073148e+000
 94   2.2736274e+000  7.9336349e-001
 95   7.6615799e+000 -1.0445564e+000
 96   2.7181608e+000  4.7615418e-001
 97   1.8291149e+000 -6.7261971e-001
 98   7.8640867e+000 -1.4296092e+000
 99   2.6362814e+000  5.8303048e-001
100   3.7771102e+000  1.2928196e+000
101   7.5360359e+000 -9.7942712e-001
102   4.0257498e+000  1.2217666e+000
103   8.4500853e+000 -7.6599648e-001
104   3.0488646e+000  6.2159289e+000
105   2.0954150e+000  2.5848825e-001
106   1.6592148e+000  7.5650162e+000
107   3.5535363e+000  1.3326217e+000
108   4.3388636e+000  2.1235893e+000
109   3.1233524e+000  1.3971470e+000
110   7.6317385e+000 -1.0744610e+000
111   8.5028402e-001 -3.2822876e-001
112   8.6903131e+000 -2.6843242e-001
113   4.4418011e+000  2.5676053e+000
114   2.5119872e+000 -1.0521242e-001
115   1.9613752e+000  7.0072931e+000
116   3.2607143e+000  1.5432286e+000
117   3.2830401e+000  1.0228031e+000
118   8.0201528e+000 -7.0827461e-001
119   3.1597313e+000  7.6750043e+000
120   9.0059933e+000 -9.6130246e-001
121   1.1037820e+000 -1.2980812e-001
122   1.5334911e+000  7.4282719e+000
123   6.0948533e-001  6.3861341e+000
124   4.0065706e-001 -1.1015776e+000
125   2.3451558e+000  8.6384057e+000
126   1.4490876e+000  8.6646066e+000
127   8.0421821e+000 -8.1100509e-001
128   8.0175747e+000 -5.6119093e-001
View Code

    to_be_quantized

 1   3.7682247e+000  8.3609865e-001
 2   2.6963398e+000  6.5766226e-001
 3   3.3438207e+000  1.2495321e+000
 4   1.3646195e+000 -6.3947640e-001
 5   7.8227583e+000 -8.8616996e-001
 6   1.3532508e+000  7.6607304e+000
 7   2.2741739e+000  6.9387226e+000
 8   3.5361382e+000  5.9729821e+000
 9   8.0409138e+000 -1.1234886e+000
10   7.9630460e+000 -1.3032200e+000
11   2.3478158e+000  6.9759690e+000
12   3.2632942e+000  1.5675470e+000
13   1.5241488e+000  7.1053147e+000
14   5.7320838e+000  3.4042655e+000
15   2.3339411e+000  6.9428434e+000
16   6.5330392e+000  3.4415860e+000
17   3.1068803e+000  8.0080363e+000
18   7.4078126e+000 -1.3416027e+000
19   1.9925474e+000 -2.7782790e-001
20   5.0187915e+000  2.7058427e+000
21   2.6535497e-001 -1.2622069e+000
22   1.4960584e+000  6.3355004e+000
23   3.1933474e-001  7.1467466e+000
24   8.2821020e+000 -9.5178778e-001
25   2.5653586e+000  6.9836115e+000
26   3.6937139e+000  1.1535671e+000
27   8.5390043e+000 -5.0678923e-001
28   7.5436898e-001 -6.7669379e-001
29   2.1638213e+000  7.6142401e+000
30   4.8522826e+000  2.7079076e+000
31   5.4890641e+000  3.3875394e+000
32   4.2525899e+000  1.8861744e+000
33   8.4088615e+000 -1.1920963e+000
34   5.5396960e+000  2.9680110e+000
35   3.3334381e+000  1.4384861e+000
36   3.5212919e+000  1.0327602e+000
37   4.6303492e+000  2.1627805e+000
38   3.9385929e+000  1.0010804e+000
39   8.4553633e+000 -7.2297277e-001
40   1.8111095e+000  7.6132396e+000
41   1.1240984e+000 -2.7029879e-001
42  -3.3840083e-002 -1.5590834e+000
43   7.1674870e+000 -1.5449905e+000
44   8.5103026e+000 -9.8820393e-001
45   7.7529857e+000 -1.4787432e+000
46   1.8704913e+000  6.9370116e+000
47   6.0271939e+000  3.2118915e+000
48   2.8287461e+000  7.3399383e+000
49   4.1568876e+000  1.5631238e+000
50   8.2187067e-001 -5.8546437e-001
51   3.1084965e+000  5.3512449e+000
52   4.1581386e+000  2.1763345e+000
53   3.2267474e+000  1.4105815e+000
54   8.1564752e-001  7.2540175e+000
55   8.0241402e+000 -8.2411742e-001
56   6.2773554e+000  3.1729045e+000
57   8.5460058e+000 -1.0330056e+000
58   8.6215210e+000 -7.4057378e-001
59   7.4872291e+000 -1.0113921e+000
60   3.3155133e+000  9.7636038e-001
61   2.1051593e+000  3.4894654e-001
62   3.6776134e+000  1.5387928e+000
63   2.9009105e+000  5.6931589e+000
64   8.0567164e+000 -1.0000803e+000
View Code

 

转载于:https://www.cnblogs.com/sea-wind/p/4544718.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/273228.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

jdbc.properties文件

drivercom.mysql.cj.jdbc.Driver (//主机:端口号、数据库名)urljdbc:mysql://localhost:3306/mydb?useSSLfalse&useUnicodetrue&characterEncodingutf8&serverTimezoneAsia/Shanghaiusername(链接的用户)passwd(密码)

ifix如何设画面大小_如何让你的视频又小又清晰?视频编码输出软件来了

如何让视频保持清晰同时又让其体积尽可能的小?这是很多小伙伴们都很头疼的问题而且很多时候我们需要会在微信或者朋友圈等社交平台上传我们的作品但它们对视频大小有着比较严格的控制所以蜜蜂菌就为大家带来了视频压缩神器小丸工具箱帮助你轻松压缩出又小又清晰的视…

智能集群理论优化控制_探索群体智能的奥妙——ROB-MAS多智能体协作

人类对未知领域总是充满好奇和敬畏。因为好奇,所以不断探索,因为敬畏,所以小心谨慎。谈到人工智能,已经不仅仅是停留在智能手机、家用电器这个层次,要知道,人工智能的作用,不仅仅是为了方便生活…

mybatis.xml文件

mybatis.xml <?xml version"1.0" encoding"UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>&l…

note同步不及时 one_朱海舟回应锤子便签同步不及时:工程师已经解决

集微网消息(文/数码控)&#xff0c;有不少网友反映自己用的锤子便签没法及时同步数据&#xff0c;对此锤子科技产品经理朱海舟回应称工程师已经把该问题解决&#xff0c;用户只需更新APP即可。至于部分安卓用户无法同步锤子便签数据的问题&#xff0c;也很好解决&#xff0c;只…

NEC学习 ---- 模块 - 带点文字链接列表

带点文字链接列表, 实现的效果是, 调整字体大小, 点的位置不会跟着变动. HTML如下: <div class"container"><div class"m-list2"><ul><li><i class"dot"></i><a href"#">带点文字链接列表…

单时隙灵敏度有什么影响_英国大学设计出低价开源单分子显微镜

低价单分子显微镜英国谢菲尔德大学的一个科学家和学生团队设计并建造了一台专业单分子显微镜&#xff0c;他们向外界分享了建造方法&#xff0c;以帮助世界各地的许多实验室使用这种设备。这台名为smfBox的显微镜能够进行单分子测量&#xff0c;让科学家一次只看一个分子&#…

(二)spring MVC配置

使用Maven添加依赖的jar包第一个还没用上 刚开始没加spring-context&#xff0c;Controller没法用 web.xml配置 1. 配置DispatcherServlet <servlet><description>Spring MVC Servlet</description><servlet-name>springMVC</servlet-name>…

练习作品13:电子试卷自动生成软件

转载于:https://www.cnblogs.com/plug/p/4558216.html

动力环境监控系统论文_机房动力环境监控系统说明

根据机房智能环境监控项目的具体要求&#xff0c;机房环境综合监控系统分为动力监控系统(对机房内的常规动力设备&#xff0c;如发电机、配电柜、UPS、蓄电池、机柜等实时监控)、环境监控系统(对温湿度、机房空调、漏水、新风系统等实时监控)和安全监控系统(对烟感传感器等系统…

ceph 存储 对比_分布式存储系统 Curve

CURVE 是网易开源的高性能、高可用、高可靠分布式存储系统&#xff0c;具有非常良好的扩展性。基于该存储底座可以打造适用于不同应用场景的存储系统&#xff0c;如块存储、对象存储、云原生数据库等。授权协议: Apache 2.0开发语言: C/C开发厂商&#xff1a;网易软件类型&…

IOS添加pch预编译文件

1.右键组名&#xff0c;New File->IOS->other选择PCH文件 2.在Build Settings的搜索栏输入prefix 3.找到Prefix Header 3.添加PCH文件&#xff0c;方法一&#xff1a;项目名/****.pch.(推荐) &#xff1b; 方法二&#xff1a;$(SRCROOT)/项目名/***.pch转载于:https://w…

前端封装接口弹出错误_项目实践:SpringBoot三招组合拳,手把手教你打出优雅的后端接口...

前言一个后端接口大致分为四个部分组成&#xff1a;接口地址&#xff08;url&#xff09;、接口请求方式&#xff08;get、post等&#xff09;、请求数据&#xff08;request&#xff09;、响应数据&#xff08;response&#xff09;。如何构建这几个部分每个公司要求都不同&am…

金算盘高手论坛资料中心_3D304期 菜鸟论坛精英PK专栏 速来围观!!

点上方“菜鸟选号论坛”→点右上角“...”→选“星标★”每日上午更新&#xff0c;星标置顶与大神不走散苹果是置顶&#xff0c;安卓是星标 点击"菜鸟选号论坛"关注我们论坛明星版块&#xff0c;集全网各路高手之大乘&#xff0c;打造草根明星 展示舞台。同时主要是为…

诸暨机器人餐厅价格_现场 | 一家尝出“锅气”的餐厅 探店机器人餐厅

观点地产网蔡澜先生的书中曾说过&#xff0c;“食在广东&#xff0c;其实是食在顺德”&#xff0c;顺德这座美食之都&#xff0c;满足了对食物异常挑剔的广东人的味蕾追求。最近&#xff0c;一家坐落“凤城”顺德的餐厅开业了。餐厅开业本身并没什么稀奇&#xff0c;不过这是一…

积木赛尔号机器人_赛尔号:谱尼三次败北,经历过被吊打、封印,一次比一次惨...

赛尔号&#xff1a;谱尼三次败北&#xff0c;经历过被吊打、封印&#xff0c;一次比一次惨。精灵类型的动漫层出不穷&#xff0c;在我们儿时看过精灵宝可梦、数码宝贝等等作品。国内的动漫则是有赛尔号和洛克王国&#xff0c;相较而言还是赛尔号更加的出名一些&#xff0c;更加…

安卓模拟器_exagear模拟器安卓模拟器

Exagear模拟器简介让Android 设备用户重温经典 PC 游戏&#xff0c;来自俄罗斯的初创公司 Eltechs发布了一款全新的 Android 游戏模拟器 ExaGear Strategies。该游戏模拟器的主要功能就是将经典的 PC 游戏带到 Android 智能手机或平板电脑上来。公告1.exagear软件和游戏&#x…

将Excle数据导入到数据库

假如Excel中的数据如下&#xff1a; 数据库建表如下&#xff1a; 其中Id为自增字段&#xff1a; 代码&#xff1a; 1 using System;2 using System.Collections.Generic;3 using System.Linq;4 using System.Web;5 using System.Web.UI;6 using System.Web.UI.WebControls;7 us…

1 睡眠唤醒_一劳永逸解决WIN10所有睡眠问题

1.初级阶段&#xff0c;解决因硬件的唤醒事件powercfg -lastwake查看上次唤醒电脑的设备哪些硬件能唤醒电脑&#xff1f;powercfg /devicequery wake_armed列出现在可以唤醒电脑的所有硬件设备&#xff0c;可以看到现在键盘能唤醒powercfg /devicequery wake_programmable列出全…

生成路径 vs 设置_Simulink代码生成之模型配置

欢迎关注我的微信公众号【新能源动力电池与BMS】&#xff0c;头条号&#xff1a;【阿Q在江湖】&#xff1b;所有文章资料会在公众号首发。本文已同步录制视频讲解&#xff1a;Simulink代码生成之模型配置_视频教程 - 知乎。PS&#xff1a;前段时间&#xff0c;传出美国禁用一些…