matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -
i want compress , decompress ecg signal stored text message text file , find out file size, cr,prd ratio,and qs.
here code compression , decompression of ecg signal using huffman algorithm. i'm trying find out file size, compression ratio,prd ratio,qs.
but error i'm getting
the ecg file in text format.
%clearing variables , screen enter code here clear all; close all; clc; [file1]=uigetfile('*.txt'); %fprintf('enter ecg file name :%s',file1); p=file1; t=sprintf('%s',p); b1=dlmread([file1]); len=length(b1); lead=input('enter lead number :'); %lead=str2double(lead); col2=b1(1:end,lead); e=fix(sqrt(len)); m=1; i=1:e j=1:e g2(i,j)=fix(col2(m)*1000); %amplifying 1000 m=m+1; end end g3=g2; i=1:e j=1:e if(g3(i,j)<0) g3(i,j)=(g3(i,j))*(-1); end end end max_term=g3(1,1); i=1:e j=1:e if(g3(i,j)>max_term) max_term=g3(i,j); end end end time=b1(1:end,1)*1000; plot(time,col2); xlabel('time / s'); ylabel('voltage / mv'); string=['input ecg signal :: ',t]; title(string); %title('input ecg signal:t'); grid on; %%%%%%%%%%%input file %%%%%%%%%%%%% fid=fopen('inputfile.txt','w+'); cnt2=fwrite(fid,col2); fclose(fid); %%%%%%a%%%%%%%%%%%%%%%%%%%%%%% a=g3; i=a; [m,n]=size(i); totalcount=m*n; %variables using find probability cnt=1; sigma=0; %computing cumulative probability. i=0:max_term k=i==i; count(cnt)=sum(k(:)) %pro array having probabilities pro(cnt)=count(cnt)/totalcount; sigma=sigma+pro(cnt); cumpro(cnt)=sigma; cnt=cnt+1; end; %symbols signal symbols = [0:max_term]; %huffman code dictionary dict = huffmandict(symbols,pro); %function converts array vector vec_size = 1; p = 1:m q = 1:n newvec(vec_size) = i(p,q); vec_size = vec_size+1; end end %huffman encodig hcode = huffmanenco(newvec,dict); %huffman decoding dhsig1 = huffmandeco(hcode,dict); %convertign dhsig1 double dhsig uint8 dhsig = uint8(dhsig1); %vector array conversion dec_row=sqrt(length(dhsig)); dec_col=dec_row; %variables using convert vector 2 array arr_row = 1; arr_col = 1; vec_si = 1; x = 1:m y = 1:n back(x,y)=dhsig(vec_si); arr_col = arr_col+1; vec_si = vec_si + 1; end arr_row = arr_row+1; end z=b1(1:end,1)*750; %converting grayscale rgb [deco, map] = gray2ind(back,256); rgb = ind2rgb(deco,map); figure,subplot(1,2,1),title('original graph'),... subplot(1,2,1),plot(b1,col2),xlabel('time / s'); figure,subplot(1,2,1),plot(time,col2),xlabel('time / s'); ylabel('voltage / mv'),grid on,title('decompressed graph'); % subplot(2,2,3),imshow((b1)),title('compressed graph'); % k=imfinfo('original.jpg'); % size_of_file=k.filesize %k=imfinfo('compressed.jpg'); %size_of_file=k.filesize %k=imfinfo('fdr.jpg'); %size_of_file=k.filesize disp('size of original ecg in bytes'); disp(bytes(p)); disp('size of ecg after compression'); fid=fopen('comp1.txt','w+'); % compressed file "comp.dat" cnt=fwrite(fid,q); fclose(fid); disp(bytes('comp1.txt')); disp('size of decompressed ecg'); fid=fopen('recons1.txt','w+'); cnt1=fwrite(fid,pro); fclose(fid); disp(bytes(pro)); % disp('size of ecg after re-construct in bytes'); % fid=fopen('recons1.dat','w+'); % cnt1=fwrite(fid,g13); % fclose(fid); % disp(bytes('recons1.dat')); %%%%%%% prd calculation %%%%%%%%%%%%% e=length(p); i=1:e y3(i)=ecg(i)- main_t(i); y4(i)=(y3(i).^2); % y5=sum(y4(i)); y5(i)=(ecg(i).^2); end y6=sum(y4)/sum(y5); prd=sqrt(y6); disp('after calculation prd:'); disp(prd); b0=bytes(p); b1=bytes('comp1.txt'); cr1=b0/b1; cr=100/cr1; disp('after calculation cr%:'); disp(cr); disp('quality score'); c=double(cr); d=double(prd); qs=double(c/(d*100)); disp(qs); %end of huffman coding
errors: code half executing , giving error in line no 189 in finding out size after compression error in line : cnt=fwrite(fid,q);
the code strangely executable if avoid moment lines
line#23 %g3=g2(i,j); line#31 %max_term=g3(1,1);
but helpful if share more precise information problem , ecg file load. mean ecg data format.