Research on simple index merging compression algorithm

Source: Internet
Author: User
Tags base64

The idea of the index merging compression algorithm is:

The index merge compression algorithm has a lossy compression method, which is mainly used to compress the integral array with certain noise. The algorithm principle is like rain, each drop will sputter a small piece of moist area, if the next drop in the wet area in front then strengthen the area, and re-calculate the wetting center, if the two areas overlap then two pieces are merged, and the wetting center is recalculated.

Algorithm Description:

The previous time Study infrared remote control learning model, because the acquisition of infrared code in the learning process is an integer array and is not standardized in numerical, it may be the cause of the noise, the resulting array will have a very small fluctuation deviation, making the garbage data content is very large. Led to the infrared code storage, transmission, analysis is very difficult. Raindrop algorithm, is the value of very close to the code value into one, and all the revised code value to establish the index table, then all the code value using the Index table subscript, after analysis and observation, found that all indexed code values are within the range of 16, The value is further compressed into half a byte, which is a half byte or char, and then attached to the transformed array before the index, together to form the encrypted infrared code. After these corrections and compression, the new code-value array length is one-eighth of the previous data length plus the length of the index table.


The Java algorithm is as follows:

Import Java.util.linkedlist;import Java.util.list;public class XiaoMiAlgRainEncode2 {static final short alg_rain_magic = (short) 0xa567;static final int wet_num =16;static final int sample_num= 1000;static final int soak_range = 26; 39//80public class Algrain {public int avr;public int total;public int min;public int Max;public int. Cnt;public Short ID X;public algrain (int val, int min, int max) {super (); This.avr = Val;this.total = Val;this.min = Min;this.max = max;this.cn t = 1;} Public Algrain () {super (); This.avr = 0;this.total = 0;this.min = 0;this.max = 0;this.cnt = 0;}  @Overridepublic String toString () {return "Algrain [avr=" + AVR + ", total=" + Total + ", min=" + min+ ", max=" + Max + ", Cnt= "+ cnt +", idx= "+ idx +"] ";}} public class Algraintable {public short m_ver = 0;public short num = 0;public int[] val= new Int[wet_num];p ublic Algrainta Ble () {super (); this.m_ver = Alg_rain_magic;this.num = 0;//system.arraycopy (val, 0, 0x00, 0, 1); for (int i = 0; i < wet_ NUM; i++) {Val[i]= 0;}}} Private list<algrain> mwets = new linkedlist<algrain> ();p rivate algraintable mArt = new algraintable (); private static Boolean Iscross (Algrain W, algrain ref) {return (Math.max (w.min, Ref.min) <= math.min (W.max, Ref.max));} private void Soak () {for (int i = 0; i < mwets.size (), i++) {algrain W1 = mwets.get (i); for (int j = i+1; J < Mwets.siz E (); J + +) {Algrain W2 = Mwets.get (j), if (Iscross (W1, W2)) {(Mwets.get (i)). Total + = W2.total; (Mwets.get (i)). cnt + = w2.cnt; Mwets.get (i)). AVR = (Mwets.get (i)). total/(Mwets.get (i)). CNT, (Mwets.get (i)). Max = Math.max ((Mwets.get (i)). Max, W2.max);(mwets.get (i)). Min = Math.max ((Mwets.get (i)). Min, w2.min); Mwets.remove (j);}}} private void Alg_rain_reset () {mwets.clear ();} private int alg_rain_feed (int sample) {Algrain w = new Algrain (sample, SAMPLE-SOAK_RANGE/2, sample + SOAK_RANGE/2); for (A Lgrain ww:mwets) {if (Iscross (WW, W)) {ww.total + = w.total;ww.cnt + = W.CNT;WW.AVR = Ww.total/ww.cnt;ww.max = Math.max (ww.ma x, W.max); ww.min = Math.max (ww. Min, w.min); return 0;}} Mwets.add (w);//soak (); return 0;} private int Alg_rain_feed_finish () {Short IDX = 0;int cnt = 0;soak (), for (Algrain w:mwets) {w.idx = Idx;mart.val[idx] = W.A vr;cnt + = w.cnt;idx++;} Mart.num = (short) cnt;mart.m_ver= alg_rain_magic;for (int i = 0; i < mwets.size (); i++) {System.out.println ("AAAAA Mwet    s["+i+"].avr= "+mwets.get (i). toString ());} return CNT;} private int Get_alg_rain_table_encode (int sample) {System.out.println ("AAAAA sample=" +sample); for (Algrain w:mwets) { System.out.println ("AAAAA w=" +w.tostring ()); if ((Sample > W.min) && (Sample < W.max) {return w.idx;}} return-1;} private int Get_alg_rain_table_decode (int idx, byte code) {byte value; if (idx%2 = = 0) {value = (byte) (0x0F & Code);} Else{value = (byte) (0x0F & Code >> 4);} return mart.val[value];} Private byte[] Alg_rain_table_encode (algraintable a) {int size = 2 + 2 + 4*wet_num; byte[] bb = new Byte[size]; System.arraycopy (Xiaomibiglittle.little_inttobyte (A.m_ver, 2), 0, BB, 0, 2); SYstem.arraycopy (Xiaomibiglittle.little_inttobyte (A.num, 2), 0, BB, 2, 2); for (int i = 0; i < Wet_num; i++) {byte[] b = X Iaomibiglittle.little_inttobyte (A.val[i], 4); for (int j = 0; J < B.length; J + +) {System.out.println ("AAAAA bbbb[" +j+ "] = "+b[j]");//system.arraycopy (B[j], 0, BB, 4+i*4+j, 1); Bb[4+i*4+j] = B[j];} System.arraycopy (b, 0, BB, 4+i, 4);} return BB;} Private algraintable Alg_rain_table_decode (byte[] b) {byte[] b1= new byte[2];byte[] b2= new byte[4]; Algraintable a = new algraintable (); System.arraycopy (b, 0, B1, 0, 2); a.m_ver = (short) xiaomibiglittle.little_bytestoint (B1); System.arraycopy (b, 2, B1, 0, 2); a.num = (short) xiaomibiglittle.little_bytestoint (B1); for (int i = 0; i < wet_num; i++) {system.arraycopy (b, 4 + i*4, B2, 0, 4); A.val[i] = Xiaomibiglittle.little_bytestoint (b2);} return A;} Public byte[] Get_alg_rain_encode (int[] insample) {int size = 2 + 2 + 4*wet_num; System.out.println ("AAAAA size" +size); byte [] Outcode = new Byte[insample.length+size]; System.out.println ("AAAAA insample.length+size "+insample.length+size); for (int i:insample) {alg_rain_feed (i);} Alg_rain_feed_finish ();//int[] tmp = new int[68]; System.arraycopy (Alg_rain_table_encode (MART), 0, Outcode, 0, size), byte b2=0, b1=0;for (int i = 0; i< insample.length; i++) {b2 = (byte) get_alg_rain_table_encode (Insample[i]); System.out.println ("AAAAA b2=" +b2+ "insample[i]=" +insample[i]); if (i%2 = = 0)//even low nibble{b1 = B2;} Else{//odd High Nibbleb1 |= B2 << 4;    System.out.println ("AAAAA b1=" +b1); OUTCODE[SIZE+I/2] = B1;}} return outcode;} Public int[] Get_alg_rain_decode (byte[] incode) {int size = 2 + 2 + 4*wet_num; Algraintable a = Alg_rain_table_decode (Incode); int[] outsmaple = new Int[a.num]; System.out.println ("AAAAA a.num=" +a.num); for (int i = 0; i < outsmaple.length; i++) {if (i%2) = = 0) {Outsmaple[i] = A.va L[incode[size+ (I/2)]&0x0f];} Else{outsmaple[i] = a.val[(incode[size+ (I/2)]>>4) &0x0f];}} return outsmaple;} public static void Main (string[] args) {//init arrayshort a1[] = New Short[100];for (int i = 0; i < a1.length; i++) {A1[i] = (short) ((i% 16) * 100 + 20); System.out.println ("AAAAA a1[" + i + "]=" + A1[i]);} Trans Intint a[] = new Int[100];for (int i = 0; i < a1.length; i++) {if ((A1[i] & 0x8000)! = 0) {A[i] = + * (i NT) (A1[i] & 0x7FFF);} else {A[i] = a1[i];} System.out.println ("AAAAA a[" + i + "]=" + A[i]);} Rain EncodeXiaoMiAlgRainEncode2 is = new XiaoMiAlgRainEncode2 (); byte[] B = Are.get_alg_rain_encode (a); for (int i = 0; i < b.length; i++) {System.out.println ("AAAAA b[" + i + "]=" + B[i]);} Base64 encodebyte[] eb64 = Xiaomibase64.base64encode (b, B.length); for (int i = 0; i < eb64.length; i++) {System.out. println ("AAAAA eb64[" + i + "]=" + Eb64[i]);} Base64 decodebyte[] db64 = Xiaomibase64.base64decode (eb64, eb64.length); for (int i = 0; i < db64.length; i++) {Syste M.out.println ("AAAAA db64[" + i + "]=" + Db64[i]);} Rain decodeint[] C = Are.get_alg_rain_decode (b); for (int i = 0; i < c.length; i++) {System.out. println ("AAAAA c[" + i + "]=" + C[i]);}  Trans Shortfor (int i = 0; i < c.length; i++) {if (C[i] > 0x8000) {a1[i] = (short) ((C[i]/+) | 0x8000);} else {A1[i] = (short) c[i];} System.out.println ("AAAAA a1[" + i + "]=" + A1[i]);} System.out.println ("AAAAA the mechine is big =" + Xiaomibiglittle.isbigendian ());}}


Research on simple index merging compression algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.