SHARE
TWEET

Untitled

a guest Mar 14th, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [y, fs] = audioread('test_whistle.m4a');
  2.  
  3. frame_len = fs/10;
  4.  
  5. foi = 440 * 2^(15/12) * (2*ones(80,1)).^((0:79)'/48); % Frequencies of interest
  6. % This uses the logarithmic relation of pitch frequencies in music
  7. % Specifically, this is all the quarter tones from high C to G above high C (in Hz)
  8.  
  9. cmat = exp(-2*pi*1i*(1/fs)*(foi * (0:frame_len-1)));
  10. % Generate look-up table for certain frequencies in the DFT.
  11. % This is akin to doing the DTFT sum for only certain frequencies.
  12. % Written slightly more legibly,
  13. % cmat = e^(-j2pi * (foi * [0, 1, ..., len - 1])/fs);
  14.  
  15. for k = 1:100:(length(y) - frame_len - 1)
  16.     range = (k + 1) : k + frame_len;
  17.     frame = y(range); % Select some samples from the audio file
  18.    
  19.     subplot(2,1,1);
  20.     plot(frame); % Plot the audio data
  21.     ylim([-1,1]); % To make it look pretty
  22.    
  23.     % frame = frame .* hann(frame_len)'; % Use a Hanning window?
  24.    
  25.     subplot(2,1,2);
  26.     spectrum = abs(cmat * frame'); % Although the matrix multiplication
  27.     % approach to calculating the DFT is n^2 complexity, I am only
  28.     % analyzing 20 frequencies. I'm not gonna do a 4800 point FFT to
  29.     % then throw away everything but 20 points.
  30.     % However, as it turns out, the FFT is still faster than this matrix
  31.     % mumbo-jumbo...
  32.    
  33.     stem(foi, spectrum, 'r'); % Plot the "discrete DFT"
  34.     ylim([0,150]); % Make it look nice
  35.     %hold on % For a sanity check, we'll overlap MATLAB's fft readout
  36.     %plot(abs(fft(frame,fs))); % Plot the fft for comparison
  37.     xlim([1000,3500]);
  38.     ylim([0,1000]); % Make it look nice    
  39.    
  40.     hold off
  41.     title(sprintf('The time now is %04.2g s', k / fs));
  42.     pause(0.0005);
  43. end
  44.  
  45. % Some problems:
  46. % I have a really crude technique here for filtering out noise. What I do
  47. % is simply set anything below a certain level to 0. This was done after
  48. % I looked at the recording myself, and determined an appropriate level.
  49. % Unfortunately, a different level is required for each recording it
  50. % seems.
  51.  
  52. % When the person whistling doesn't whistle exactly on pitch, the detected
  53. % note is spurious. Furthermore, if their volume modulates, sometimes you
  54. % get a kind of "switch bounce" artefact (clearly seen with test_whistle3,
  55. % at around 2 seconds). We need a robust method for tolerating these kinds
  56. % of imperfections, so that the machine doesn't play a bunch of wrong notes
  57. % on the xylophone.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top