Research on simple index merge Compression Algorithms

Source: Internet
Author: User

Research on simple index merge Compression Algorithms

The idea of the index merge compression algorithm is:

The index merge compression algorithm has a lossy compression algorithm, which is mainly used to merge and compress integer arrays containing certain noise. The principle of the algorithm is like rain. Each drop will flat a small wet area. If the next drop falls in the wet area above, the area will be strengthened and the wet center will be re-computed; if the two parts overlap, the two parts are merged and the wet center is recalculated.

Algorithm Description:

Some time ago I studied the infrared remote control learning model. Because the infrared code collected during the learning process is an integer array and the value is not standardized, it may be caused by noise, the obtained integer array has a small fluctuation deviation, resulting in a large amount of junk data. As a result, it is very difficult to store, transmit, and analyze infrared codes. The raindrops algorithm combines code values that are very close to each other into one, creates an index table with all the corrected code values, and then uses the subscript of the index table to represent all the code values, after analysis and observation, it is found that all indexed code values are within the range of 16, so the value is further compressed to half a byte, that is, half a byte or char, then, the index table is attached to the transformed array to form an encrypted infrared code. After these correction and compression, the length of the new code value array is 1/8 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 idx;public AlgRain(int val, int min, int max) {super();this.avr = val;this.total = val;this.min = min;this.max = max;this.cnt = 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];public AlgRainTable() {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 mWets = new LinkedList();private 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.size(); 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 (AlgRain 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.max, 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.avr;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 mWets["+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 = XiaoMiBigLittle.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.val[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] = 100 * (int) (a1[i] & 0x7FFF);} else {a[i] = a1[i];}System.out.println("AAAAA a[" + i + "]=" + a[i]);}// rain EncodeXiaoMiAlgRainEncode2 are = 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++) {System.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] / 100) | 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());}}


Related Article

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.