Search code examples
matlabaudiosignal-processingguitar

How to use envelope function in Matlab


The following Matlab code generate a sound but it contain tak tak like sound which can be easily heard.Can someone removes this noise by using envelope function.

  Fs=44100;
  T=1/Fs;
  M=zeros(1,88);
  for I=7:88
    M(I)=round(36.8*(2^(1/12))^(I-6));
    end
    signal=[];
    FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
    49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
    50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
    45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
    50,6];
    for i=1:length(FrTm)
      x=FrTm(i,1);
      y=FrTm(i,2);
      F=M(x);
      time=0:1/Fs:y/18;
      sinewave=*sin(2*pi*10*F*time);

      signal=[signal sinewave];

     end

     stem(signal)
     sound(signal,Fs)

Solution

  • Here are two possible solutions. They are not perfect since the tak-tak is still here but it is much lower.

    1. Applying a high-pass filter to remove the annoying sound which seems to be low frequency

      Fs=44100;
      T=1/Fs;
      M=zeros(1,88);
      for I=7:88
          M(I)=round(36.8*(2^(1/12))^(I-6));
      end
      signal=[];
      FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
      49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
      50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
      45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
      50,6];
      for i=1:length(FrTm)
          x=FrTm(i,1);
          y=FrTm(i,2);
          F=M(x)/4;
          time=0:1/Fs:y/18;
          sinewave=sin(2*pi*10*F*time);
      
          signal=[signal sinewave];
      
      end
      
      % Creating high-pass filter with passband frequency of 1000 Hz (may be too high)
      hpFilt = designfilt('highpassiir','FilterOrder',8, ...
          'PassbandFrequency',1000,'PassbandRipple',0.2, ...
      'SampleRate',Fs);
      % Filtering sound
      signal = filter(hpFilt,signal);
      
      sound(signal,Fs)
      
    2. Taking the last value from each generated sine wave and finding the corresponding phase for the next sine wave, to make the sinus as continuous as possible

       Fs=44100;
       T=1/Fs;
       M=zeros(1,88);
       for I=7:88
           M(I)=round(36.8*(2^(1/12))^(I-6));
       end
       signal=[];
       FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
         49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
      50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
      45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
      50,6];
       previous_end_val = 0;
       for i=1:length(FrTm)
           x=FrTm(i,1);
           y=FrTm(i,2);
           F=M(x)/4;
           time=0:1/Fs:y/18;
      
           % Calculating required phase to match the end of previous sinewave
           phi = asin(previous_end_val);
           % Generating sinewave with phase phi
           sinewave=sin(2*pi*10*F*time + phi);
           previous_end_val = sinewave(end);
      
           signal=[signal sinewave];
      
       end
      
       sound(signal,Fs)
      

    The first solution is not perfect: if you take too high passband frequency you could not hear the lowest audible frequencies. The second solution is faster and more elegant. Please ask if there is a method you don't understand.