已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
% KSVD running file% in this file a synthetic test of the K-SVD algorithm is performed. First,% a random dictionary with normalized columns is being generated, and then% a set of data signals, each as a linear combination of 3 dictionary% element is created, with noise level of 20SNR. this set is given as input% to the K-SVD algorithm. % a different mode for activating the K-SVD algorithm is until a fixed% error is reached in the Sparse coding stage, instead until a fixed number of coefficients is found% (it was used by us for the% denoising experiments). in order to switch between those two modes just% change the param.errorFlag (0 - for fixed number of coefficients, 1 -% until a certain error is reached). param.L = 3; % number of elements in each linear combination.param.K = 50; % number of dictionary elementsparam.numIteration = 50; % number of iteration to execute the K-SVD algorithm. param.errorFlag = 0; % decompose signals until a certain error is reached. do not use fix number of coefficients.%param.errorGoal = sigma;param.preserveDCAtom = 0; % creating the data to train on %N = 1500; % number of signals to generaten = 20; % dimension of each dataSNRdB = 20; % level of noise to be addedparam.TrueDictionary, D, x = gererateSyntheticDictionaryAndData(N, param.L, n, param.K, SNRdB); % % initial dictionary: Dictionary elements %param.InitializationMethod = DataElements; param.displayProgress = 1;disp(Starting to train the dictionary); Dictionary,output = KSVD(D,param); disp(The KSVD algorithm retrived ,num2str(output.ratio(end), atoms from the original dictionary); Dictionary,output = MOD(D,param); disp(The MOD algorithm retrived ,num2str(output.ratio(end), atoms from the original dictionary);function Dictionary, data, coefs = gererateSyntheticDictionaryAndData(N, L, dim, K, SNRdB) randn(state,sum(100*clock);rand(state,sum(100*clock); Dictionary = randn(dim,K);Dictionary = Dictionary*diag(1./sqrt(sum(Dictionary.*Dictionary); data,coefs = CreateDataFromDictionarySimple(Dictionary, N, L); if (SNRdB=0) | (SNRdB = 80) returnelse noise = randn(size(data); actualNoise = calcNoiseFromSNR(SNRdB,data, noise); SNR = calcSNR(data, data+actualNoise); data = data + actualNoise*SNR/SNRdB; end function D,xOrig = CreateDataFromDictionarySimple(dictionary, numElements, numCoef)maxRangeOfCoef = 1;resolution = 0.0001; xOrig = zeros(size(dictionary,2),numElements);%vecOfValues = -1*maxRangeOfCoef:resolution:maxRangeOfCoef;%coefs = randsrc(numCoef,numElements,vecOfValues);coefs = randn(numCoef,numElements)*maxRangeOfCoef;xOrig(1:numCoef,:) = coefs;for i=1:size(xOrig,2) xOrig(:,i) = xOrig(randperm(size(xOrig,1),i);end%dictionaryElementIndices = randsrc(numCoef*numElements,1,1:size(dictionary,2) ; %matrixOfIndices = repmat(1:numElements,numCoef,1);%xOrig(sub2ind(size(xOrig),dictionaryElementIndices,matrixOfIndices(:) = coefs;D = dictionary*xOrig; function actualNoise = calcNoiseFromSNR(TargerSNR, signal, randomNoise)signal = signal(:);randomNoiseRow = randomNoise(:);signal_2 = sum(signal.2);ActualNoise_2 = signal_2/(10(TargerSNR/10);noise_2 = sum(randomNoiseRow.2);ratio = ActualNoise_2./noise_2;actualNoise = randomNoiseRow.*repmat(sqrt(ratio),size(randomNoiseRow,1),1);actualNoise = reshape(actualNoise,size(randomNoise); function SNR = calcSNR(origSignal, noisySignal)errorSignal = origSignal-noisySignal;signal_2 = sum(origSignal.2);noise_2 = sum(errorSignal.2); SNRValues = 10*log10(signal_2./noise_2);SNR = mean(SNRValues);function Dictionary,output = KSVD(. Data,. % an nXN matrix that contins N signals (Y), each of dimension n. param)% =% K-SVD algorithm% =% The K-SVD algorithm finds a dictionary for linear representation of% signals. Given a set of signals, it searches for the best dictionary that% can sparsely represent each signal. Detailed discussion on the algorithm% and possible applications can be found in The K-SVD: An Algorithm for % Designing of Overcomplete Dictionaries for Sparse Representation, written% by M. Aharon, M. Elad, and A.M. Bruckstein and appeared in the IEEE Trans. % On Signal Processing, Vol. 54, no. 11, pp. 4311-4322, November 2006. % =% INPUT ARGUMENTS:% Data an nXN matrix that contins N signals (Y), each of dimension n. % param structure that includes all required% parameters for the K-SVD execution.% Required fields are:% K, . the number of dictionary elements to train% numIteration,. number of iterations to perform.% errorFlag. if =0, a fix number of coefficients is% used for representation of each signal. If so, param.L must be% specified as the number of representing atom. if =1, arbitrary number% of atoms represent each signal, until a specific representation error% is reached. If so, param.errorGoal must be specified as the allowed% error.% preserveDCAtom. if =1 then the first atom in the dictionary% is set to be constant, and does not ever change. This% might be useful for working with natural% images (in this case, only param.K-1% atoms are trained).% (optional, see errorFlag) L,. % maximum coefficients to use in OMP coefficient calculations.% (optional, see errorFlag) errorGoal, . % allowed representation error in representing each signal.% InitializationMethod,. mehtod to initialize the dictionary, can% be one of the following arguments: % * DataElements (initialization by the signals themselves), or: % * GivenMatrix (initialization by a given matrix param.initialDictionary).% (optional, see InitializationMethod) initialDictionary,. % if the initialization method % is GivenMatrix, this is the matrix that will be used.% (optional) TrueDictionary, . % if specified, in each% iteration the difference between this dictionary and the trained one% is measured and displayed.% displayProgress, . if =1 progress information is displyed. If param.errorFlag=0, % the average repersentation error (RMSE) is displayed, while if % param.errorFlag=1, the average number of required coefficients for % representation of each signal is displayed.% =% OUTPUT ARGUMENTS:% Dictionary The extracted dictionary of size nX(param.K).% output Struct that contains information about the current run. It may include the following fields:% CoefMatrix The final coefficients matrix (it should hold that Data equals approximately Dictionary*output.CoefMatrix.% ratio If the true dictionary was defined (in% synthetic experiments), this parameter holds a vector of length% param.numIteration that includes the detection ratios in each% iteration).% totalerr The total representation error after each% iteration (defined only if% param.displayProgress=1 and% param.errorFlag = 0)% numCoef A vector of length param.numIteration that% include the average number of coefficients required for representation% of each signal (in each iteration) (defined only if% param.displayProgress=1 and% param.errorFlag = 1)% = if (isfield(param,displayProgress) param.displayProgress = 0;endtotalerr(1) = 99999;if (isfield(param,errorFlag)=0) param.errorFlag = 0;end if (isfield(param,TrueDictionary) displayErrorWithTrueDictionary = 1; ErrorBetweenDictionaries = zeros(param.numIteration+1,1); ratio = zeros(param.numIteration+1,1);else displayErrorWithTrueDictionary = 0; ratio = 0;endif (param.preserveDCAtom0) FixedDictionaryElement(1:size(Data,1),1) = 1/sqrt(size(Data,1);else FixedDictionaryElement = ;end% coefficient calculation method is OMP with fixed number of coefficients if (size(Data,2) 1 & param.displayProgress) if (param.errorFlag=0) output.totalerr(iterNum-1) = sqrt(sum(sum(Data-FixedDictionaryElement,Dictionary*CoefMatrix).2)/prod(size(Data); disp(Iteration ,num2str(iterNum), Total error is: ,num2str(output.totalerr(iterNum-1); else output.numCoef(iterNum-1) = length(find(CoefMatrix)/size(Data,2); disp(Iteration ,num2str(iterNum), Average number of coefficients: ,num2str(output.numCoef(iterNum-1); end end if (displayErrorWithTrueDictionary ) ratio(iterNum+1),ErrorBetweenDictionaries(iterNum+1) = I_findDistanseBetweenDictionaries(param.TrueDictionary,Dictionary); disp(strcat(Iteration , num2str(iterNum), ratio of restored elements: ,num2str(ratio(iterNum+1); output.ratio = ratio; end Dictionary = I_clearDictionary(Dictionary,CoefMatrix(size(FixedDictionaryElement,2)+1:end,:),Data); if (isfield(param,waitBarHandle) waitbar(iterNum/param.counterForWaitBar); endend output.CoefMatrix = CoefMatrix;Dictionary = FixedDictionaryElement,Dictionary;% findBetterDictionaryElement% function betterDictionaryElement,CoefMatrix,NewVectorAdded = I_findBetterDictionaryElement(Data,Dictionary,j,CoefMatrix,numCoefUsed)if (length(who(numCoefUsed)=0) numCoefUsed = 1;endrelevantDataIndices = find(CoefMatrix(j,:); % the data indices that uses the jth dictionary element.if (length(relevantDataIndices)1) %(length(relevantDataIndices)=0) ErrorMat = Data-Dictionary*CoefMatrix; ErrorNormVec = sum(ErrorMat.2); d,i = max(ErrorNormVec); betterDictionaryElement = Data(:,i);%ErrorMat(:,i); % betterDictionaryElement = betterDictionaryElement./sqrt(betterDictionaryElement*betterDictionaryElement); betterDictionaryElement = betterDictionaryElement.*sign(betterDictionaryElement(1); CoefMatrix(j,:) = 0; NewVectorAdded = 1; return;end NewVectorAdded = 0;tmpCoefMatrix = CoefMatrix(:,relevantDataIndices); tmpCoefMatrix(j,:) = 0;% the coeffitients of the element we now improve are not relevant.errors =(Data(:,relevantDataIndices) - Dictionary*tmpCoefMatrix); % vector of errors that we want to minimize with the new element% % the better dictionary element and the values of beta are found using svd.% % This is becaus
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 景区连片开发协议书
- 养老院入住协议书范本
- 汽车销售合作协议书
- 仓储库房建设协议书
- 2025年短视频创作者合作协议协议
- 2025年短视频创作者分成合同(季度)
- 平安前端笔试题库及答案
- 2025年机遇:无人机娱乐产业在低空经济中的发展报告
- 低空经济行业2025监管事权划分与跨部门协同创新模式探索报告
- 2025年电缆买卖合同范本
- 基坑外架专项施工方案(单立杆双排脚手架)
- 2025年基本级执法资格考试真题试卷及答案
- 安全文明驾驶培训教案课件
- 国家能源集团笔试试题及答案
- 本科护理系毕业论文
- 第6章 绩效反馈(《绩效管理》第3版)
- 沈阳康莱德酒店设计方案
- 2025年工行私人银行考试题库
- 2025年上海市公务员考试行测试卷历年真题参考答案详解
- 美的历程全集讲解
- 2022版10kV及以下业扩受电工程技术导则
评论
0/150
提交评论