Exp1 Merged
Exp1 Merged
clear all;
close all;
% Record audio
disp('Start speaking:');
recordblocking(recorderObj, 5);
disp('Recording finished:');
recordedAudio = getaudiodata(recorderObj);
soundsc(recordedAudio, recorderObj.SampleRate);
filename = 'recordedVoice.wav';
choice = menu('Choose an action:', 'Plot Audio Waveform', 'Plot Pitch Contour', 'Calculate Average Pitch', 'Calculate Total Energy', 'Calculate
Energy within Time Window');
switch choice
case 1
plotAudioWaveform(y, Fs);
case 2
plotPitchContour(y, Fs);
case 3
calculateAveragePitch(y, Fs);
case 4
calculateTotalEnergy(y);
case 5
otherwise
disp('Invalid choice');
end
end
figure;
plot(t, y);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Audio Waveform');
grid on;
xlim([0 length(y)/Fs]);
ylim([-1,1]);
end
figure;
plot(pitch);
xlabel('Time (s)');
ylabel('Pitch (Hz)');
end
function calculateAveragePitch(y, Fs)
avg_pitch = mean(pitch);
disp('Average Pitch:');
disp(avg_pitch);
end
function calculateTotalEnergy(y)
energy = sum(y.^2);
disp('Total Energy:');
disp(energy);
end
windowed_audio = y(round(t_start*Fs):round(t_end*Fs));
windowed_energy = sum(windowed_audio.^2);
disp(windowed_energy);
end
MATLAB Code-:
clc;
clear all;
close all;
fd = 0.025; % Frame Duration
[data, fs] = audioread('abc.wav');
f_size = fd*fs; %Frame Size
fr = data(9600:9600+f_size); % For Single Frame
figure;
subplot(2,1,1)
plot(fr);
xlabel("Freq");
title("Single Frame");
fr_win = fr.*hamming(length(fr));
stft = fft(fr_win);
stft=abs(stft);
subplot(2,1,2)
plot(fr_win)
hold on;
plot(fr);
% xlabel("Freq");
% title("fr_window");
%hold on;
subplot(2,1,2)
plot(stft);
xlabel("Freq");
title("STFT");
fd = 0.025;
[data, fs] = audioread('abc.wav');
f_size = fd*fs;
fr = data(2000:2000+f_size);
subplot(2,1,1)
plot(fr);
xlabel("Freq");
title("Single Frame");
%fr_win = fr.*hamming(length(fr));
zcr = sum(diff(sign(fr))>0)/2; % for phase angle
%stzcr = zcr/f_size
subplot(2,1,2)
%plot(stzcr);
xlabel("Freq");
title("STZCR");
Output-:
stzcr =
0.0425
MATLAB Code-:
clc;
clear all;
close all;
fd = 0.05;
[data, fs] = audioread('abc.wav');
f_size = fd*fs;
subplot(5,2,1)
plot(data);
title("Waveform");
xlabel("Time");
ylabel("Pitch");
fr1 = data(10000:10000+f_size);
subplot(5,2,2)
plot(fr1)
title("Single frame voiced");
xlabel("Time");
ylabel("Pitch");
fr2 = data(11600:11600+f_size);
subplot(5,2,3)
plot(fr2)
title("Single frame unvoiced");
xlabel("Time");
ylabel("Pitch");
fr_win1 = fr1.*hamming(length(fr1));
stft1 = fft(fr_win1);
subplot(5,2,4)
plot(abs(stft1))
title("Short term FT voiced");
xlabel("Freq");
fr_win2 = fr2.*hamming(length(fr2));
stft2 = fft(fr_win2);
subplot(5,2,5)
plot(abs(stft2))
title("Short term FT unvoiced");
xlabel("Freq");
lg1 = log(abs(stft1));
lg2 = log(abs(stft2));
subplot(5,2,6)
plot(lg1)
title("Log Plot voiced");
xlabel("Freq");
subplot(5,2,7)
plot(lg2)
title("Log Plot unvoiced");
xlabel("Freq");
isft1 = ifft(lg1);
isft2 = ifft(lg2);
subplot(5,2,8)
plot(isft1)
title("Cepstral Voiced");
xlabel("time");
subplot(5,2,9)
plot(isft2)
title("Cepstral unvoiced");
xlabel("time");
figure
plot(isft1)
title("Cepstral Voiced");
xlabel("time");
Output-:
STE:
% do framing
f_d = 0.025; % in seconds
frames = framing(data,fs,f_d);
ste_wave = 0;
for j = 1 : length(ste)
l = length(ste_wave);
ste_wave(l : l + f_size) = ste(j);
end
STA:
clc;
%clear all;
close all;
% normalize data
% do framing
f_d = 0.025;
[r,c]=size(frames);
for k=1 :r
t = frames (k,:) ;
sum1 = 0;
ac=0 ;
for i=0:length(t)-1
sum1 = 0;
for j = 1:length(t)-i
s=t(j)*t(j+i) ;
sum1 = sum1 + s;
end
ac(i+1)=sum1;
end
temp(k)=ac(1);
end
% short term auto corr. (stac) stac = stac./max(stac); %normalize the data
stac_wave = 0;
for j = 1: length(stac)
l = length(stac_wave);
end
for j = 1: length(stac)
l = length(stac_wave);
end
t = t(1:end - 1);
t1 = 0: 1/fs: length(stac_wave)/fs;
t1 = t1(1:end - 1);
Silence Removal:
close all;
clc;
y = Y(:,1); % It's a stereo signal. Hence, only one channel is enough to work with
figure;
plot(t, y, 'r', 'linewidth', 2);
energy_envelope = zeros(size(y));
for i = 1:frame_length:length(y)-frame_length+1
frame = y(i:i+frame_length-1);
energy_envelope(i:i+frame_length-1) = sum(frame.^2);
end
%% Silence removal
figure;
plot(t_new, y_new, 'b', 'linewidth', 2);