0% found this document useful (0 votes)
12 views

Exp1 Merged

The code processes an audio signal to remove silence. It reads in an audio file, calculates the short-term energy envelope, sets a threshold based on this, and removes sections where the envelope is below the threshold, outputting a new signal without silence.

Uploaded by

DEEPAK PRAJAPATI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Exp1 Merged

The code processes an audio signal to remove silence. It reads in an audio file, calculates the short-term energy envelope, sets a threshold based on this, and removes sections where the envelope is below the threshold, outputting a new signal without silence.

Uploaded by

DEEPAK PRAJAPATI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

function main()

clear all;

close all;

% Record audio

recorderObj = audiorecorder(16000, 16, 1);

disp('Start speaking:');

recordblocking(recorderObj, 5);

disp('Recording finished:');

recordedAudio = getaudiodata(recorderObj);

% Play recorded audio

soundsc(recordedAudio, recorderObj.SampleRate);

disp('Playing the recording. Close the figure to stop');

% Save recorded audio

audiowrite('recordedVoice.wav', recordedAudio, 32000);

disp('Recording saved to "recordedVoice.wav"');

% Load recorded audio

filename = 'recordedVoice.wav';

[y, Fs] = audioread(filename);

% Perform different tasks based on user input

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

calculateEnergyWithinTimeWindow(y, Fs, 0.5, 0.75);

otherwise

disp('Invalid choice');

end

end

function plotAudioWaveform(y, Fs)

t = linspace(0, length(y)/Fs, length(y));

figure;

plot(t, y);

xlabel('Time (seconds)');

ylabel('Amplitude');

title('Audio Waveform');

grid on;

xlim([0 length(y)/Fs]);

ylim([-1,1]);

end

function plotPitchContour(y, Fs)

[pitch,~] = pitch(y, Fs);

figure;

plot(pitch);

xlabel('Time (s)');

ylabel('Pitch (Hz)');

title('Pitch Contour of Speech');

end
function calculateAveragePitch(y, Fs)

[pitch,~] = pitch(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

function calculateEnergyWithinTimeWindow(y, Fs, t_start, t_end)

windowed_audio = y(round(t_start*Fs):round(t_end*Fs));

windowed_energy = sum(windowed_audio.^2);

disp('Energy within time window:');

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:

[data, fs] = audioread('recordedVoice.wav');


%normalize data
data = data / abs(max(data));

% do framing
f_d = 0.025; % in seconds
frames = framing(data,fs,f_d);

%calculate frame energy


[r,c] = size(frames);
ste = 0;
for i=1:r
ste(i) = sum(frames(i,:).^2);
end

ste = ste./max(ste); %normalize the data

f_size = round(f_d * fs);

ste_wave = 0;

for j = 1 : length(ste)
l = length(ste_wave);
ste_wave(l : l + f_size) = ste(j);
end

% Plot the STE with signal


t = [0 : 1/fs : length(data)/fs]; %time in seconds
t = t(1:end-1);
t1 = [0 : 1/fs : length(ste_wave)/fs];
t1 = t1(1:end-1);

plot(t, data'); hold on;


plot(t1, ste_wave, 'r', 'LineWidth',2);
legend('Speech Signal', 'Short Term Energy (frame Energy');

STA:

clc;

%clear all;
close all;

[data, fs] = audioread('recordedVoice.wav');

% normalize data

data =data / abs (max(data));

% do framing

f_d = 0.025;

frames = framing(data, fs, f_d); % it is like 0% over

% Short term autocorrelation and taking 1 coeff. val

[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);

stac(k)=ac (2); % taking second coeff. built in fun.


temp(k)=ac(1);

stac(k)=ac (2); % taking second coeff. built in fun

% autocorrelation (it is autocorr at lag 1)

end

% short term auto corr. (stac) stac = stac./max(stac); %normalize the data

f_size = round(f_d * fs);

stac_wave = 0;

for j = 1: length(stac)

l = length(stac_wave);

stac_wave(l: l + f_size) = stac(j);

end

for j = 1: length(stac)

l = length(stac_wave);

stac_wave(1: 1 + f_size) = stac(j);

end

% plot the stac with Signal

t = 0: 1/fs: length(data)/fs; % time in sec

t = t(1:end - 1);

t1 = 0: 1/fs: length(stac_wave)/fs;

t1 = t1(1:end - 1);

plot(t, data'); hold on;

plot(t1, stac_wave, 'r', 'LineWidth', 2);

legend('Speech Signal', 'Short Term Auto Corr.');

Silence Removal:
close all;
clc;

%% Reading the audio file to be silence removed


[Y, fs] = audioread('recordedVoice.wav'); %fs: sampling rate. y: is audio signal. It is a stereo signal

y = Y(:,1); % It's a stereo signal. Hence, only one channel is enough to work with

%% plotting this input signal

t = (0:length(y)-1) / fs; % time vector

figure;
plot(t, y, 'r', 'linewidth', 2);

title('Input audio signal (with silent portion)');


xlabel('Time (s)');
ylabel('Amplitude');
grid on;

%% Compute short-term energy envelope

frame_duration = 0.02; % shorter frame duration for better resolution


frame_length = round(frame_duration * fs);

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

%% Set dynamic threshold

threshold_factor = 5; % adjust as needed

Threshold = threshold_factor * mean(energy_envelope(energy_envelope > 0));

%% Silence removal

y_new = y(energy_envelope > Threshold);

%% plotting the silence removed portion

t_new = (0:length(y_new)-1) / fs; % time vector for the new signal

figure;
plot(t_new, y_new, 'b', 'linewidth', 2);

title('Silence removed version of the audio signal');


xlabel('Time (s)');
ylabel('Amplitude');
grid on;

You might also like