tsounakis

Speech Analysis 2.3

Nov 24th, 2021 (edited)
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.11 KB | None | 0 0
  1. function [] = speechFundamentalFrequency()
  2.     clc;
  3.     clear;
  4.     [rawSpeech, Fs] = audioread("speech.wav");
  5.     timeSamplingWindow = 20*10^(-3);
  6.    
  7.     sample = rawSpeech(round(length(rawSpeech)/2-timeSamplingWindow*Fs:round(length(rawSpeech)/2+timeSamplingWindow*Fs)));
  8.    
  9.     %plotTime(sample, Fs, "Male Speech Sample (Time Domain)");
  10.     plotSample(sample, "Male Speech Sample (Samples)", 1);
  11.    
  12.     pitch = [];
  13.     pitch = findPitch(sample, Fs, pitch, "Time-domain plot method:" + "\n");
  14.     autocorrelationSample = xcorr(sample, sample);
  15.     plotSample(autocorrelationSample, "Autocorrelation of Sample Waveform", 2);
  16.     pitch = findPitch(autocorrelationSample, Fs, pitch, "\n" + "Autocorrelation algorithm method:" + "\n");
  17.    
  18.     annotation('textbox', [0.08 0.05 0.83 0.3], 'interpreter','latex','String', pitch)
  19. end
  20.  
  21. function reply = findPitch(signal, Fs, reply, status)
  22.     [pks, ~] = sortrows(findpeaks(signal, Fs), 1, 'descend');
  23.     if status == "Time-domain plot method:" + "\n"
  24.         time = sprintf(status + "Distance between two peaks is equal to " + num2str((find(signal == pks(1))) - (find(signal == pks(2)))) + " samples.\nTime between these two peaks is equal to " + num2str(((find(signal == pks(1))) - (find(signal == pks(2))))*10^3/Fs) + " m$s$.\n");
  25.         status = "";
  26.     else
  27.         time = [];
  28.     end
  29.     cross = sprintf(status + "Fundamental frequency ($f_0$) is equal to " + num2str(Fs/((find(signal == pks(1))) - (find(signal == pks(2))))) + " $Hz$.\n");    
  30.     reply = sprintf("%s%s%s", reply, time, cross);
  31. end
  32.  
  33. function [] = plotSample (data, ntitle, n)
  34.     figure(1)
  35.     subplot(3,1,n);
  36.     stem(data, 'color', [0 0 0] +0.1, 'Marker', '.');
  37.     xlim([0, length(data)]);
  38.     grid on;
  39.     xlabel('Samples');
  40.     ylabel('Amplitude');
  41.     title(ntitle);
  42. end
  43.  
  44. function [] = plotTime (data, Fs, ntitle, n)
  45.     figure(1)
  46.     subplot(3,1,n);
  47.     t = linspace(0, length(data)/Fs, length(data));
  48.     stem(t, data, 'color', [0 0 0] +0.1, 'Marker', '.');
  49.     xlim([0, length(t)/Fs]);
  50.     grid on;
  51.     xlabel('Time [sec]');
  52.     ylabel('Amplitude');
  53.     title(ntitle);
  54. end
Add Comment
Please, Sign In to add comment