Canmv k230 案例5.1——小波变换(一)
1)Haar 小波
尺度函数与小波函数
Haar 可以先用
a=[1 1]
b=[1 -1]
表示,暂时不考虑正交系数
2)小波变换公式![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/944b8ce196da6a1e4189656a99f1d291.png#pic_center)
公式可以理解为一个信号与小波在时间上的积分,考虑到实际的信号是离散的,而小波函数的长度有限,可以考虑在FIR滤波器的角度去理解
小波是一个窗函数,与小波长度的信号截断做卷积和,然后在信号上滑动,遍历整个信号,因为采用的尺度函数和小波函数的不同,可以得到信号的低频和高频分量
3)信号产生与分解
在canmv k230中暂时没有找到随机数,因此采用MATLAB生成数据
clear all
clc
L=512;
t=1:L;
x=sin(2*pi*t/L);
r=0.2*(rand(1,L)-0.5);
xr=x+r;
plot(xr)
噪声信号如下
将生成数据添加到canmv k230,根据Haar小波编写函数如下
4)canmv k230测试
# Haar by 花月
# 原始数据 含噪声的单周期信号
xr=[0.0308431361713272,-0.0483621911007796,0.00825595785277677,0.0523667769395261,0.154496944656249,0.0188815606454048,0.124282709145283,0.0773507258497612,0.100079749356844,0.117447501508957,0.171625363046010,0.107743469854706,0.131618804211670,0.165484697179684,0.179120936534232,0.290040419140827,0.262296257188618,0.151326400054157,0.327207389191509,0.179653634828601,0.272470713080759,0.275244030405369,0.184993054244937,0.195685627300112,0.355990681588430,0.257489673431377,0.279435114523237,0.288987337715393,0.281855207802769,0.260128774640620,0.423387528020142,0.479723342075777,0.396892272451706,0.418980236621560,0.333782581208239,0.390102461040203,0.474410221625786,0.455082339286513,0.391202507241405,0.464059585734722,0.425854094623544,0.477550009591375,0.462670776518196,0.438356676378322,0.552415480214668,0.575158895601357,0.483137146419181,0.584346900944698,0.553892976974352,0.497014178743782,0.632263023475544,0.529915038280537,0.545499777025255,0.662756407315375,0.667948454489407,0.568973548869362,0.583040817724807,0.722603405536817,0.740193709782328,0.622635794266290,0.757545050710250,0.641057605039318,0.685842808255069,0.787301632468397,0.722348848944981,0.776867585816232,0.658890299343419,0.827850621254677,0.749656722554072,0.806475157659305,0.708247683685975,0.708064448545249,0.822701735387931,0.872192322840442,0.705955536649881,0.851446894925100,0.872885667580056,0.762117639849347,0.860989721164276,0.758904240502041,0.779992611454981,0.772069996454548,0.900411458689756,0.762719000584853,0.898481849080714,0.849803705915754,0.862131931981765,0.939234331592888,0.970004861799878,0.987628224605221,0.907218425322904,0.989216501864418,0.889689628856931,0.841156254788946,0.826002729375521,0.887221844387213,0.923995593002250,0.886264549286122,0.945983560974367,0.931954440618568,1.00476365321546,0.992523043653640,0.892429692443595,0.996345589042235,0.893410946426832,1.06367301367427,0.929005158066226,0.963913772176885,0.959033497259517,0.942359660326367,0.934577149662372,1.00828623882057,0.979964615043634,0.974534195862250,0.935364876573725,0.962278574166872,0.923695492568377,1.08344323698496,1.05513608085609,0.986535335759684,0.926082377949621,0.961699993157049,0.935178763532216,0.987418980721901,1.00081223065458,0.947717099749541,1.05266889779795,1.08860586458409,0.979357275546261,1.03109939818068,0.962551046696387,0.912375899627206,0.901112487550002,1.05901327494989,0.942733029858029,1.03298482745061,0.922481445093797,1.05432222344017,0.948318365205280,0.899819874498136,1.04085802542860,1.02286558184793,0.980409216601653,1.06033690759960,0.938772224237998,0.998561189370120,1.05908871420018,1.04222968492102,0.872109702473796,0.917370761790475,1.00560177395224,0.878059971702124,0.914195506190328,0.999858664720154,1.03566738671516,0.891595444350901,0.889951781853659,0.990070484022730,0.987357660874323,0.841548748209081,0.935614064859551,0.890370567517549,0.968185397390461,0.974295474392637,0.935038097075656,0.949334722612653,0.866294783769703,0.931316670393481,0.883135518783400,0.933036104514033,0.791126227691924,0.929580407306002,0.864716240730462,0.761387034128584,0.836859800642553,0.925744457923914,0.731882636706032,0.885542821005671,0.868769517111687,0.769491961740050,0.847399795095460,0.860185838536974,0.798113520557376,0.798091900478228,0.698358569451217,0.770450955807688,0.675582121585912,0.814614254359726,0.688218249166625,0.659378956997043,0.699970873107088,0.716800199399297,0.770742484167445,0.676533433505680,0.607329200014194,0.672171431259828,0.716036516059989,0.642044140426333,0.729427042697014,0.611347873101980,0.529453433804004,0.640550355080403,0.630639020233500,0.682810402463495,0.500175547199633,0.558746526968180,0.630057435580024,0.484351483217427,0.532355749334816,0.481852812769475,0.582285431507836,0.584806841141575,0.420081071750349,0.485243355714382,0.435375468784272,0.553421617707345,0.532064781841187,0.380098202284796,0.520961324946677,0.337902773733295,0.449351955557017,0.415038495710100,0.388115651215602,0.314572313409555,0.362629270710943,0.312573397617051,0.427639206288716,0.368260935648365,0.257635206161633,0.317201920721411,0.305166252183025,0.353587801699507,0.237507696723772,0.169825427402591,0.348538856768138,0.227500040611113,0.132755685746112,0.312134802054031,0.285536515031953,0.283247625170824,0.141764175099611,0.172618278870170,0.237541805387876,0.116666997632201,0.0669511824222754,0.113166880087967,0.0629454034128342,0.192506850896254,0.0518798731575030,0.163982906962511,0.0936565908711546,0.131195711397472,-0.0378826866147103,-0.0124772845483251,0.0592780107700727,0.0782781495505684,0.00798496942685197,-0.0915044559607463,-0.0577332355492654,0.0226779284347735,0.00973027029227913,-0.0430150781311424,-0.0931965111332919,-0.184903722124911,-0.0803635346035952,-0.109662056012781,-0.204717146718513,-0.0571906102613564,-0.113486546315973,-0.203604747550928,-0.258902107185118,-0.227655558601721,-0.266929568711661,-0.232112033345818,-0.298757399334393,-0.284467647335788,-0.271934850579118,-0.185133315950513,-0.202218278390677,-0.231287246716825,-0.363941440993085,-0.276695083443126,-0.388151731353754,-0.256637982282036,-0.422887156981777,-0.356852563506728,-0.342574431088482,-0.326968364861077,-0.342233765233307,-0.499280563800109,-0.486606769212950,-0.519100686547456,-0.340060641474877,-0.516668272328692,-0.554160498524077,-0.461744491123313,-0.518227288820997,-0.417553250151374,-0.564513299232299,-0.555750142349318,-0.502427729663190,-0.603275877836629,-0.533513094712777,-0.588834335227704,-0.636622066281224,-0.539166399731428,-0.640595986709008,-0.553433053142983,-0.628114588688050,-0.654670448444580,-0.679793338423652,-0.660370313187643,-0.652847419519067,-0.596971176375086,-0.756417033036276,-0.605186468639300,-0.628918718721302,-0.755345146424872,-0.716475312999551,-0.623898879736141,-0.680379652091046,-0.817639139443178,-0.818998729840195,-0.732433865719608,-0.725564794538150,-0.777529923851049,-0.771742005414802,-0.781613969100467,-0.697934334190705,-0.855032757706120,-0.747485606970437,-0.789208773551761,-0.847915595525376,-0.903677125512746,-0.747377413277984,-0.776692865111169,-0.769275594472124,-0.884305946011328,-0.911001429512985,-0.940627046192172,-0.846860253708912,-0.903188999498586,-0.843829846691871,-0.959377555589546,-0.943824014407512,-0.873762737816009,-0.927577855051839,-0.915175463830954,-0.809451433497704,-0.983366076428067,-0.821682857029443,-0.915659833042526,-1.02504874792322,-0.868818824190143,-1.02694586049244,-0.886413739349344,-0.982326929237939,-0.913998376367856,-1.03712541611408,-0.954277071376494,-0.937797430504656,-0.967209995072916,-0.918578433829555,-0.929402826484008,-1.03586146579212,-0.976742035342854,-1.06481039164191,-1.02653341135208,-0.883893888728776,-0.891923095468961,-1.08332536965248,-0.978310707243486,-0.903865942515473,-0.962254615511794,-0.940641887056944,-1.07820377552910,-0.936181243831558,-1.02093474725771,-1.08753803118834,-0.940190533161373,-1.07000600124333,-0.958718144826118,-0.910588638397833,-0.980991044245388,-0.992450337960795,-0.933737756950737,-0.922452997696324,-1.02618010897554,-1.08079737068599,-1.08739422111655,-1.00380883075446,-1.08894848084636,-0.914995002058890,-0.996625071432829,-1.00482856528397,-0.938250276218399,-0.983585845593423,-0.909836517476187,-1.08023844708929,-0.913203694635382,-1.07776191811756,-1.02998174381002,-1.06139501984797,-1.01417230972858,-0.905212880894380,-0.908489031557766,-0.972870323509447,-0.976958006181152,-1.00442711062644,-1.01653951515616,-0.942093110834802,-1.00822295410262,-1.00941256793628,-0.942888726025613,-0.945593932193636,-0.964044170629399,-0.918101322903619,-1.01272295844092,-0.950729594838723,-0.996361851067830,-0.874155845339737,-0.978846625234328,-0.956802261137125,-0.955872171513125,-0.894891407044921,-0.858424392744757,-0.812182957396109,-0.949861548935915,-0.811976561599500,-0.797747897224354,-0.878388216012553,-0.747008115082799,-0.806601681216502,-0.895008322346774,-0.833106860266819,-0.807028717652905,-0.705033205320671,-0.855184883779178,-0.786606993113176,-0.825708746052048,-0.830580468776692,-0.736368572224310,-0.724683715622893,-0.698374916941452,-0.641113199664360,-0.702088017820293,-0.770922165871238,-0.752908454750169,-0.709510974812386,-0.780092171887084,-0.749736790496110,-0.757493957605859,-0.578354215619049,-0.724611426896499,-0.671529433199805,-0.555445529292078,-0.592808465590343,-0.589982877274448,-0.514021432452329,-0.581783713525234,-0.628552105560290,-0.645668620490781,-0.470963079476716,-0.525315454565126,-0.536293337621600,-0.518916839682034,-0.444017282950370,-0.574608987388887,-0.566485974719025,-0.524819425640316,-0.494971875015287,-0.397078291617797,-0.553492032334114,-0.350116611549183,-0.456968428757448,-0.365239019319111,-0.326620862532602,-0.497851307182725,-0.413551802936278,-0.462105319207949,-0.311883510730610,-0.455755386196126,-0.315026614810429,-0.340390276261047,-0.307350609349062,-0.369334167463448,-0.235649815896486,-0.293990415163651,-0.267880800959806,-0.295259258262254,-0.293841932755943,-0.298391688442659,-0.212226791715846,-0.180116879808326,-0.162312016547278,-0.229668813975730,-0.133405198245468,-0.259018524990865,-0.154803573747912,-0.227266828456598,-0.199667769653783,-0.0871534930302471,-0.0394580765981781,-0.0534747048854794,-0.166888625738847,0.0147090699563672,-0.0658711163196536,0.0321875780163484,-0.0738640563338988,0.0301248781423040,0.0565466129099269,0.0793678107220675]
print(xr)
# 低通滤波器
Haar1=[1/2,1/2]
# 高通滤波器
Haar2=[1/2,-1/2]
xh1=[] # 低频信号
xh2=[] # 高频信号
for i in range(len(xr)-len(Haar1)/2):
xH1=xr[i]*Haar1[0]+xr[i+1]*Haar1[1] # 积之和
xh1.append(xH1)
xH2=xr[i]*Haar2[0]+xr[i+1]*Haar2[1] # 积之和
xh2.append(xH2)
print(len(xh1))
print(xh1)
print(len(xh2))
print(xh2)
5)测试结果
分解结果
频谱结果
频谱比较,记得输出信号补0,保持与原始长度相同
因为Haar小波不连续,所以计算结果并不是特别好,有什么疑问可以留言哦