In: Electrical Engineering
What would I add to the following code to detect the pitch of the audio signal? (use cepstral method)
clear all;
close all;
clc;
info = audiodevinfo;
info.input(1)
info.input(2)
pause(3); % delete it if necessary
clc;
fs = 44.1e3;
noBits = 16;
noChannels = 1;
recordObject = audiorecorder(fs,noBits,noChannels);
disp('Start speaking...');
record(recordObject);
pause(3);
stop(recordObject);
disp('End of Recording.');
x = getaudiodata(recordObject); %takin in as aperiodic
signal
noSamples = length(x); %length of vector x "max(size(x))
time = [1:noSamples]/fs; %time ticks
%DFT: X[k]= (sigma x[n]*exp(-j*2*pi*k/Nf*n)
noSamples = length(x2)
noFrequencies = noSamples;
for ct =1:noSamples
X = nan(size(x2));
for k = 1:noFrequencies
X(k) = sum(x2.*exp(-j*2*pi*k/noFrequencies*n));
end
figure;
subplot(2,1,1);
kAsFrequencyIndicies = 1:noFrequencies;
plot(kAsFrequencyIndicies,abs(X));
box off;
subplot(2,1,2);
frequencyTicks = kAsFrequencyIndicies/noFrequencies*fs;
plot(frequencyTicks,abs(X));
box off;
This code is in MATLAB, It records a signal using audiorecorder function.I recorded a signal where i repeatedly kept saying "A". This code extracts pitch and formant using Cepstral Analysis. At the end it prints letter "A" in Notepad.
Here is a code and the screenshot of it:
clc rec = audiorecorder(1000,16,1); disp('Start speaking.') recordblocking(rec, 2); disp('End of Recording.'); y = getaudiodata(rec); w=hamming(2000); c=y.*w; x=fft(c); e=log(abs(x)); g=ifft(e); d=g(1:length(g)/2); l=[ones(1,25) zeros(1,975)]; M=transpose(d); f=M.*l; %subplot(4,1,2); %plot(f); p=f(1:25); %subplot(4,1,3); %plot(p); s=fft(p,8000); %subplot(4,1,4); %plot(abs(s)); z=s(1:4000); %subplot(4,1,1); %plot(real(z)); k=1; for i=2:length(z)-1; if(z(i-1)<z(i)) & (z(i+1)<z(i)) formant_mag(k)=z(i); formant(k)=i; k=k+1; else continue; end end %plot(real(z)); %hold on %plot(formant,formant_mag,'r*'); %hold off n=fft(z); %plot(abs(n)); l2=[zeros(1,25) ones(1,975)]; p=l2.'; M=real(p.*d); %plot(q) [t_val,t_loc]=max(M); pitch_period=t_loc pitch_frequency=(1/pitch_period)*1000 b=['a']; fid=fopen('word2.txt','w'); fprintf(fid,'%c',b); fclose(fid); type word2.txt status=dos('notepad word2.txt')
This code doesn't perform Noise filtering.