Advertisement
_Mizanur

Information Theory and Coding

Feb 4th, 2023
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.06 KB | None | 0 0
  1. 1.Lempel-Ziv Code
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define pb push_back
  5. #define all(a) (a).begin(),(a).end()
  6. int main()
  7. {
  8.    string text="AABABBBABAABABBBABBABB";
  9.    vector<string>v;
  10.    string s;
  11.    for(int i=0;i<text.size();i++)
  12.    {
  13.        s+=text[i];
  14.        if(!count(all(v),s))
  15.        {
  16.            v.pb(s);
  17.            s.clear();
  18.        }
  19.  
  20.    }
  21.    v.pb("BB");
  22.  
  23.  int k=1;
  24. unordered_map<string,int>mp;
  25.    for(auto u:v)
  26.    {
  27.        mp[u]=k;
  28.        cout<<k<<" "<<u<<endl;
  29.        k++;
  30.    }
  31.    mp["BB"]=7;
  32.  
  33.    vector<pair<int,char>>vp;
  34.    for(auto u:v)
  35.    {
  36.        string ss;
  37.        if(u.size()==1)
  38.        {
  39.            char uv=u[0];
  40.             vp.pb({100,uv});
  41.        }
  42.        else{
  43.  
  44.       for(int i=0;i<u.size()-1;i++)
  45.       {
  46.          ss+=u[i];
  47.       }
  48.       char ch=u[u.size()-1];
  49.       if(mp[ss])
  50.       {
  51.         vp.pb({mp[ss],ch});
  52.       }
  53.       }
  54.    }
  55.    vector<string>vs;
  56.  
  57.    for(auto u:vp)
  58.       {
  59.           string x;
  60.           cout<<u.first<<u.second<<endl;
  61.           if(u.first==100){
  62.           x+=bitset<3>(0).to_string();
  63.           }
  64.           else
  65.           {
  66.             x+=bitset<3>(u.first).to_string();
  67.           }
  68.           if(u.second=='A')
  69.             x+='0';
  70.           else
  71.             x+='1';
  72.           vs.pb(x);
  73.       }
  74.       for(auto u:vs)
  75.         cout<<u<<endl;
  76.     return 0;
  77. }
  78.  
  79. 2.Hamming Code
  80. //1011011
  81. //2
  82. #include<bits/stdc++.h>
  83. using namespace std;
  84. int main()
  85. {
  86.     cout<<"Enter the 7 bit data sequence:";
  87.     string str;
  88.     cin>>str;
  89.     reverse(str.begin(),str.end());
  90.     cout<<"Enter parity state:";
  91.     int state;
  92.     cin>>state;
  93.     vector<int>pos,newpos;
  94.     for(int i=1;i<=7;i++)
  95.     {
  96.        if((i&(i-1))==0)
  97.             pos.push_back(i-1);
  98.     }
  99.     reverse(pos.begin(),pos.end());
  100.     newpos=pos;
  101.     vector<int>pp={4,5,6,2,5,6,2,4,6};
  102.     int sz=pp.size();
  103.     string ss;
  104.     int val=0;
  105.     for(int i=0;i<3;i++)
  106.     {
  107.         int cnt=0;
  108.  
  109.         for(int j=0;j<3;j++)
  110.         {
  111.             if(str[pp[sz-1]]=='1')
  112.             {
  113.                 cnt++;
  114.             }
  115.                 pp.pop_back();
  116.                 sz=pp.size();
  117.  
  118.         }
  119.         if((cnt+(str[newpos[val]]-'0'))%2!=0)
  120.             {
  121.                 ss+='1';
  122.             }
  123.             else
  124.             {
  125.                 ss+='0';
  126.             }
  127.         pos.pop_back();
  128.         val++;
  129.  
  130.     }
  131.     unsigned long long value =bitset<64>(ss).to_ullong();
  132.     if(value>0)
  133.     {
  134.         cout<<"There is an error in this sequence and the position is:"<<value<<endl;
  135.         if(str[value-1]=='1')
  136.             str[value-1]='0';
  137.         else
  138.             str[value-1]='1';
  139.         reverse(str.begin(),str.end());
  140.         cout<<"And the correct sequence is:"<<str<<endl;
  141.     }
  142.     else
  143.     {
  144.         cout<<"There is no error in this code sequence"<<endl;
  145.     }
  146.     return 0;
  147. }
  148.  
  149. 3.Huffman Coding
  150. #include<bits/stdc++.h>
  151. using namespace std;
  152. struct Node{
  153.     int data;
  154.     struct Node* left;
  155.     struct Node* right;
  156.  
  157.     Node(int val)
  158.     {
  159.         data=val;
  160.         left=NULL;
  161.         right=NULL;
  162.     }
  163.  
  164. };
  165. struct cmp{
  166. bool operator()(Node* l,Node* r)
  167. {
  168.     return (l->data > r->data);
  169. }
  170.  
  171. };
  172. void preorder(Node *root,string s,vector<string>&ans)
  173. {
  174.     if(!root)
  175.         return;
  176.     if(!root->left && !root->right)
  177.     {
  178.         ans.push_back(s);
  179.     }
  180.     preorder(root->left,s+"0",ans);
  181.     preorder(root->right,s+"1",ans);
  182. }
  183. vector<string>huffman(string s,vector<int>f,int n)
  184. {
  185.     priority_queue<Node*,vector<Node*>,cmp>mh;
  186.     for(int i=0;i<n;i++)
  187.     {
  188.         Node *temp=new Node(f[i]);
  189.         mh.push(temp);
  190.     }
  191.     while(mh.size()!=1)
  192.     {
  193.         Node *left=mh.top();
  194.         mh.pop();
  195.         Node *right=mh.top();
  196.         mh.pop();
  197.         Node *parent=new Node(left->data+right->data);
  198.         parent->left=left;
  199.         parent->right=right;
  200.         mh.push(parent);
  201.     }
  202.     Node *root=mh.top();
  203.     mh.pop();
  204.     vector<string>ans;
  205.     preorder(root,"",ans);
  206.     return ans;
  207. }
  208. int main()
  209. {
  210.     ///abcde
  211.     ///7 9 2 15 3
  212.     string s;
  213.     cout<<"Enter the string:";
  214.     cin>>s;
  215.     int n=s.size();
  216.     vector<int>f(n);
  217.     cout<<"Enter the corresponding frequency:";
  218.     for(int i=0;i<n;i++)
  219.     {
  220.         cin>>f[i];
  221.     }
  222.     vector<string>ans=huffman(s,f,n);
  223.     vector<char>vc={'d','b','c','d','a'};
  224.     int k=0;
  225.     for(auto u:ans)
  226.     {
  227.         cout<<vc[k]<<" = "<<u<<endl;
  228.         k++;
  229.     }
  230.     return 0;
  231. }
  232.  
  233. 4.Optimality
  234. //abcde
  235. 7 9 2 15 3
  236. #include<bits/stdc++.h>
  237. using namespace std;
  238. struct Node{
  239.  
  240.     int data;
  241.     struct Node* left;
  242.     struct Node* right;
  243.  
  244.     Node(int val)
  245.     {
  246.         data=val;
  247.         left=NULL;
  248.         right=NULL;
  249.     }
  250.  
  251. };
  252.  
  253. struct cmp{
  254.  
  255.     bool operator()(Node* l,Node* r)
  256.     {
  257.         return (l->data > r->data);
  258.     }
  259. };
  260. void preorder(Node *root,string s,vector<string>&ans)
  261. {
  262.     if(!root)
  263.         return;
  264.     if(!root->left && !root->right)
  265.     {
  266.         ans.push_back(s);
  267.     }
  268.     preorder(root->left,s+"0",ans);
  269.     preorder(root->right,s+"1",ans);
  270. }
  271. vector<string>huffman(string s,vector<int>f,int n)
  272. {
  273.     priority_queue<Node*, vector<Node*>,cmp>mh;
  274.     for(int i=0;i<n;i++)
  275.     {
  276.         Node *temp=new Node(f[i]);
  277.         mh.push(temp);
  278.     }
  279.     while(mh.size()!=1)
  280.     {
  281.         Node *left=mh.top();
  282.         mh.pop();
  283.         Node *right=mh.top();
  284.         mh.pop();
  285.         Node *parent=new Node(left->data+right->data);
  286.         parent->left=left;
  287.         parent->right=right;
  288.         mh.push(parent);
  289.     }
  290.     Node *root=mh.top();
  291.     mh.pop();
  292.     vector<string>ans;
  293.     preorder(root,"",ans);
  294.     return ans;
  295. }
  296. int main()
  297. {
  298.     cout<<"Enter a string:";
  299.     string s;
  300.     cin>>s;
  301.     int n=s.size();
  302.     vector<int>f(n);
  303.     for(int i=0;i<n;i++)
  304.     {
  305.         cin>>f[i];
  306.     }
  307.     vector<string>ans=huffman(s,f,n);
  308.   double arr[n];
  309.   int k=0;
  310.   double cnt=0;
  311.     for(auto u:ans)
  312.     {
  313.         arr[k]=u.size();
  314.         cnt+=arr[k];
  315.     }
  316.     double sum=0;
  317.     for(int i=0;i<n;i++)
  318.     {
  319.        sum+=1/pow(2,arr[k]);
  320.     }
  321.     if(sum<=1){
  322.         cout<<"optimal"<<endl;
  323.         for(auto u:ans)
  324.             cout<<u<<endl;
  325.     }
  326.     else
  327.         cout<<"Not Optimal"<<endl;
  328.  
  329.     cout<<endl;
  330.     return 0;
  331. }
  332.  
  333.  
  334. 5.Channel Capacity
  335. #include<bits/stdc++.h>
  336. using namespace std;
  337.  
  338. int main()
  339. {
  340.     int row,col;
  341.     cout<<"Enter the number of rows and columns:";
  342.     cin>>row>>col;
  343.     double arr[row][col];
  344.     for(int i=0;i<row;i++)
  345.     {
  346.         for(int j=0;j<col;j++)
  347.         {
  348.             cin>>arr[i][j];
  349.         }
  350.     }
  351.     double H_of_Y_given_X;
  352.     H_of_Y_given_X=arr[0][0]*log2(1/arr[0][0])+arr[0][1]*log2(1/arr[0][1]);
  353.     cout<<"Conditional Probability:"<<H_of_Y_given_X<<" bits/message symbol"<<endl;
  354.     double capacity=1-H_of_Y_given_X;
  355.     cout<<"Capacity = "<<capacity<<" bits/message symbol"<<endl;
  356.  
  357.     return 0;
  358. }
  359.  
  360. 6.Joint Distribution
  361. #include<bits/stdc++.h>
  362. using namespace std;
  363. int main()
  364. {
  365.     int row,col;
  366.     cout<<"Enter rows and columns:";
  367.     cin>>row>>col;
  368.  
  369.     ///Take the values of matrix
  370.     cout<<"Enter the values of joint distribution:"<<endl;
  371.     double arr[row][col];
  372.  
  373.     for(int i=0;i<row;i++)
  374.     {
  375.         for(int j=0;j<col;j++)
  376.         {
  377.             cin>>arr[i][j];
  378.         }
  379.     }
  380.     double x[row],y[col];
  381.     cout<<"Enter the marginal distribution of X:";
  382.     for(int i=0;i<row;i++)
  383.         cin>>x[i];
  384.  
  385.     cout<<"Enter the marginal distribution of Y:";
  386.     for(int i=0;i<col;i++)
  387.         cin>>y[i];
  388.  
  389.         ///Calculation H(X)
  390.     double H_of_X=0;
  391.     for(int i=0;i<row;i++)
  392.     {
  393.         H_of_X+=(-x[i]*log2(x[i]));
  394.     }
  395.     cout<<"H(X) = "<<H_of_X<<" bits"<<endl;
  396.         ///Calculation H(Y)
  397.     double H_of_Y=0;
  398.     for(int i=0;i<col;i++)
  399.     {
  400.         H_of_Y+=(-y[i]*log2(y[i]));
  401.     }
  402.     cout<<"H(Y) = "<<H_of_Y<<" bits"<<endl;
  403.  
  404.     ///Calculation of H(X|Y)
  405.  
  406.     double H_of_X_given_Y=0,H_of_Y_given_X=0;
  407.     double Hx[row];
  408.     for(int i=0;i<row;i++){
  409.             double val2=0;
  410.     for(int j=0;j<col;j++)
  411.     {
  412.         double val1=(arr[i][j]*(1/y[i]));
  413.     if(val1==0)
  414.         val2+=0;
  415.     else
  416.         val2+=(-val1*log2(val1));
  417.     }
  418.     Hx[i]=val2;
  419.     }
  420.     for(int i=0;i<row;i++)
  421.     {
  422.         H_of_X_given_Y+=(y[i]*(Hx[i]));
  423.     }
  424.     cout<<"H(X|Y) = "<<H_of_X_given_Y<<" bits"<<endl;
  425.  
  426.      ///Calculation of H(Y|X)
  427.  
  428.     double Hy[row];
  429.     for(int i=0;i<row;i++){
  430.             double val2=0;
  431.     for(int j=0;j<col;j++)
  432.     {
  433.         double val1=(arr[j][i]*(1/x[i]));
  434.     if(val1==0)
  435.         val2+=0;
  436.     else
  437.         val2+=(-val1*log2(val1));
  438.     }
  439.     Hy[i]=val2;
  440.     }
  441.     for(int i=0;i<row;i++)
  442.     {
  443.         H_of_Y_given_X+=(x[i]*(Hy[i]));
  444.     }
  445.     cout<<"H(Y|X) = "<<H_of_Y_given_X<<" bits"<<endl;
  446.     return 0;
  447. }
  448.  
  449. 7.Entropy
  450.  
  451. #include<bits/stdc++.h>
  452. using namespace std;
  453. int main()
  454. {
  455.     vector<double>v[4];
  456.     v[0]={1,2,1};
  457.     v[1]={1,1};
  458.     v[2]={2,1,1};
  459.     v[3]={1,1};
  460.     double cnt=0;
  461.     vector<double>vv;
  462.     for(int i=0;i<4;i++)
  463.     {
  464.         int tmp=0;
  465.         for(auto u:v[i])
  466.         {
  467.            tmp+=u;
  468.         }
  469.         vv.push_back(tmp);
  470.         cout<<tmp<<" ";
  471.         cnt+=tmp;
  472.     }
  473.     cout<<endl;
  474.     double mu=0;
  475.     double h_mu=0;
  476.     for(int i=0;i<4;i++)
  477.     {
  478.         for(auto u:v[i])
  479.         {
  480.             h_mu+=-((u/cnt)*log2(u/cnt));
  481.         }
  482.     }
  483.     cout<<h_mu<<endl;
  484.     for(auto u:vv)
  485.     {
  486.         mu+=-((u/cnt)*log2(u/cnt));
  487.     }
  488.     cout<<mu<<endl;
  489.  
  490.     cout<<h_mu-mu<<endl;
  491.  
  492.  
  493.  
  494.     return 0;
  495. }
  496.  
  497.  
  498. 8.Convolutional Code
  499. //1011
  500. #include<bits/stdc++.h>
  501. using namespace std;
  502. typedef long long ll;
  503. typedef vector<int> vi;
  504.  
  505. #define rapid_io() ios::sync_with_stdio(false);cin.tie(0);
  506. #define endl '\n'
  507. #define pb push_back
  508. #define all(a) (a).begin(),(a).end()
  509. #define rall(a) (a).rbegin(),(a).rend()
  510.  
  511. int main()
  512. {
  513.     //rapid_io();
  514.     string str;
  515.     cout<<"Enter the code sequence:";
  516.     cin>>str;
  517.     str+="000";
  518.     str="00"+str;
  519.     vector<int>res;
  520.     int v1,v2;
  521.     for(int i=str.size()-1;i>=2;i--)
  522.     {
  523.         v1=(str[i]-'0')^(str[i-1]-'0')^(str[i-2]-'0');
  524.         res.pb(v1);
  525.         v2=(str[i]-'0')^(str[i-2]-'0');
  526.         res.pb(v2);
  527.     }
  528.  
  529.     for(auto u:res)
  530.         cout<<u;
  531.     cout<<endl;
  532.  
  533.  
  534.  
  535.     return 0;
  536. }
  537.  
  538. lempel-ziv
  539.  
  540. #include<bits/stdc++.h>
  541. using namespace std;
  542. #define pb push_back
  543. #define all(a) (a).begin(),(a).end()
  544. int main()
  545. {
  546.    string text="000101110010100101";
  547.    vector<string>v;
  548.    v.pb("0");
  549.    v.pb("1");
  550.    string s;
  551.    for(int i=0;i<text.size();i++)
  552.    {
  553.        s+=text[i];
  554.        if(!count(all(v),s))
  555.        {
  556.            v.pb(s);
  557.            s.clear();
  558.        }
  559.  
  560.    }
  561.    for(auto u:v)
  562.    {
  563.        cout<<u<<endl;
  564.    }
  565.    //v.pb("BB");
  566.  
  567.  
  568.  
  569.  int k=1;
  570. unordered_map<string,int>mp;
  571.    for(auto u:v)
  572.    {
  573.        mp[u]=k;
  574.        cout<<k<<" "<<u<<endl;
  575.        k++;
  576.    }
  577.  
  578.    //mp["BB"]=7;
  579.  
  580.    vector<pair<int,int>>vp;
  581.    for(auto u:v)
  582.    {
  583.        string ss;
  584.        if(u.size()==1)
  585.        {
  586.            if(u=="0")
  587.            vp.pb({0,1});
  588.            else
  589.             vp.pb({1,2});
  590.  
  591.        }
  592.        else{
  593.  
  594.       for(int i=0;i<u.size()-1;i++)
  595.       {
  596.          ss+=u[i];
  597.       }
  598.       int ch=(u[u.size()-1])-'0';
  599.       if(ch==1)
  600.       {
  601.         vp.pb({mp[ss],2});
  602.       }
  603.       else{
  604.         vp.pb({mp[ss],1});
  605.       }
  606.       }
  607.    }
  608.    vector<string>vs;
  609.     int flag=1;
  610.    for(auto u:vp)
  611.       {
  612.           string x;
  613.           cout<<u.first<<" "<<u.second<<endl;
  614.           if(u.first==0){
  615.           x+=bitset<3>(0).to_string();
  616.           }
  617.           else if(u.first==1 && flag==2)
  618.           {
  619.               x+=bitset<3>(0).to_string();
  620.           }
  621.           else if(u.first==1)
  622.           {
  623.               x+=bitset<3>(1).to_string();
  624.           }
  625.           else
  626.           {
  627.             x+=bitset<3>(u.first).to_string();
  628.           }
  629.           if(u.second==1)
  630.             x+='0';
  631.           else
  632.             x+='1';
  633.           vs.pb(x);
  634.           flag++;
  635.       }
  636.       for(auto u:vs)
  637.         cout<<u<<endl;
  638.     return 0;
  639. }
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement