Advertisement
Guest User

Untitled

a guest
May 21st, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.60 KB | None | 0 0
  1. function [output] = audiomod(input,rate,fs)
  2.     %audio: audio signal to be modified (e.g., speech or music).
  3.     %output: output audio
  4.     %rate: Playback speed, rate > 0
  5.     %fs: sample rate
  6.    
  7.     %Audio signal prep
  8.     [audio,fs] = audioread(input);
  9.    
  10.     %Experiments with Block Length led us to a BlockLength of 1000
  11.     %and a hop length of 200 for the best outputs. This results in
  12.     %an overlap of about 80%.
  13.     iBlockLength = 1000;
  14.     iHopLength=iBlockLength/5;  
  15.     numBlocks = floor(length(audio)/iBlockLength);
  16.     fftlen= 2*iBlockLength;
  17.    
  18.     %Keep track of the Phase of the audio signal
  19.     phase=zeros(1,iBlockLength);
  20.    
  21.     %Set Output to appropriate length with some additional padding
  22.     output=zeros(1,floor(length(audio)/rate+iBlockLength));
  23.    
  24.     %Iterate through the signal at appropriate rate of hopLength * rate
  25.     for i = 1:round(iHopLength*rate):length(audio)-(iBlockLength+iHopLength)+1
  26.         %Two windows of samples
  27.         win1=audio(i:i+iBlockLength-1);
  28.         win2=audio(i+iHopLength:i+iBlockLength-1+iHopLength);
  29.        
  30.         %FFT of each window, element wise multiplication to hamming windows
  31.         s1=fft(win1.*hamming(iBlockLength))';
  32.         s2=fft(win2.*hamming(iBlockLength))';
  33.        
  34.         %Modify the phase in accordance to the iteration
  35.         phase=mod((phase+angle(s1./s2)),2*pi);
  36.        
  37.         %Synthesis
  38.         ablock=real(ifft(abs(s1).*exp(1j*phase)))';
  39.         n=round(i/rate);
  40.         output(n:n+iBlockLength-1)= output(n:n+iBlockLength-1) + (ablock.*hamming(iBlockLength))';
  41.     end
  42.     soundsc(output,fs);
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement