title: basic - Trying to add noise to an Audio file and trying to reduce errors using basic coding such as Repeatition code
body: <p>We were recently taught the concepts of error control coding - basic codes such as Hamming code, repeatition code etc.</p>
  
  <p>I thought of trying out these concepts in MATLAB. My goal was to compare how an audio file plays when corrupted by noise and in the case when the file is protected by basic codes and then corrupted by noise.</p>
  
  <p>So I opened a small audio clip of 20-30 seconds in MATLAB using audioread function. I used 16 bit encoded PCM wave file. 
  If opened in 'native' format it is in int16 format . If not it opens in a double format.</p>
  
  <p>I then added two types of noises to it : - AWGN noise (using double format) and Binary Symmetric Channel noise (by converting the int16 to uint16 and then by converting that to binary using dec2bin function). Reconverting back to the original int16 format does add a lot of noise to it.</p>
  
  <p>Now my goal is to try out a basic repeatition code. So what I did was to convert the 2-d audio file matrix which consists of binary data into a 3-d matrix by adding redundancy. I used the following command : -</p>
  
  <p>cat(3,x,x,x,x,x) ; </p>
  
  <p>It created a 3-D matrix such that it had 5 versions of x along the 3rd dimension.</p>
  
  <p>Now I wish to add noise to it using bsc function. 
  Then I wish to do the decoding of the redundant data by removing the repetition bits using a mode() function on the vector which contains the redundant bits.</p>
  
  <p>My whole problem in this task is that MATLAB is taking too long to do the computation. I guess a 30 second file creates quite a big matrix so maybe its taking time. Moreover I suspect what I am doing is not the most efficient way to do it with regards to the various data types.</p>
  
  <p>Can you suggest a way in which I may improve on the computation times. Are there some functions which can help do this basic task in a better way.</p>
  
  <p>Thanks.
  (first post on this site with respect to MATLAB so bear with me if the posting format is not upto the mark.)</p>
  
  <p>Edit - posting the code here :-</p>
  
  <pre><code>[x,Fs] = audioread('sample.wav','native'); % native loads it in int16 format , Fs of sample is 44 khz , size of x is 1796365x1
  x1 = x - min(x); % to make all values non negative
  s = dec2bin(x); % this makes s as a 1796365x15 matrix the binary stream stored as character string of length 15. BSC channel needs double as the data type
  s1 = double(s) - 48; % to get 0s and 1s in double format
  </code></pre>
  
  <p>%% Now I wish to compare how noise affects s1 itself or to a matrix which is error control coded.</p>
  
  <pre><code>s2 = bsc(s,0.15); % this adds errors with probability of 0.15
  s3 = cat(3,s,s,s,s,s) ; % the goal here is to add repetition redundancy. I will try out other efficient codes such as Hamming Code later.
  s4 = bsc(s3,0.15);% this step is taking forever and my PC is unresponsive because of this one.
  s5 = mode(s4(,,:)) ; % i wish to know if this is a proper syntax, what I want to do is calculate mode along the 3rd dimension just to remove redundancy and thereby reduce error.
  </code></pre>
  
  <p>%% i will show what I did after s was corrupted by bsc errors in s2,</p>
  
  <pre><code> d = char(s2 + 48);
   d1 = bin2dec(d) + min(x);
   sound(d1,Fs); % this plays the noisy file. I wish to do the same with error control coded matrix but as I said in a previous step it is highly unresponsive.
  </code></pre>
  
  <p>I suppose what is mostly wrong with my task is that I took a large sampling rate and hence the vector was very big.</p>