用HUFFMAN算法压缩和减压心电信号

时间:2015-04-23 17:50:13

标签: matlab compression huffman-code

我想压缩和解压缩存储为文本消息的ecg信号文本文件,并找出文件大小,cr,prd ratio和qs。

这是我使用霍夫曼算法压缩和解压缩ecg信号的代码。我试图找出文件大小,压缩率,prd比率,qs。

但这是我得到的错误

  

ecg文件最初是文本格式。

        %clearing all variables and screen

        enter code here

        clear all;

        close all;

        clc;

        [file1]=uigetfile('*.txt');

        %fprintf('Enter the 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;

         for i=1:e

         for j=1:e

         g2(i,j)=fix(col2(m)*1000); %Amplifying by 1000

         m=m+1;

         end

         end

         g3=g2;


         for i=1:e

         for j=1:e


         if(g3(i,j)<0)


          g3(i,j)=(g3(i,j))*(-1);

          end

          end

          end

          max_term=g3(1,1);


           for i=1:e


           for 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 to find the probability

         cnt=1;




         sigma=0;



         %computing the cumulative probability.


         for i=0:max_term

         k=I==i;

         count(cnt)=sum(k(:))


         %pro array is having the probabilities


         pro(cnt)=count(cnt)/Totalcount;


         sigma=sigma+pro(cnt);


         cumpro(cnt)=sigma;

         cnt=cnt+1;

         end;

        %Symbols for the signal

        symbols = [0:max_term];


        %Huffman code Dictionary

        dict = huffmandict(symbols,pro);

           %function which converts array to vector

         vec_size = 1;

          for p = 1:m

          for 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 to dhsig uint8

           dhsig = uint8(dhsig1);


           %vector to array conversion

            dec_row=sqrt(length(dhsig));



            dec_col=dec_row;

            %variables using to convert vector 2 array

            arr_row = 1;

            arr_col = 1;

            vec_si = 1;

              for x = 1:m

              for 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 to 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);

              for 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 the huffman coding

错误: 我的代码执行了一半,然后在第89行中发出错误,在压缩后查找大小,特别是错误在此行中: CNT = fwrite的(FID,Q);

1 个答案:

答案 0 :(得分:0)

如果您暂时避免使用某些行

,则代码可以执行得很奇怪
line#23    %g3=g2(i,j);
line#31    %max_term=g3(1,1);

但如果您分享有关问题的更准确信息以及要加载的ecg文件,将会有所帮助。我的意思是心电数据格式。