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

DSP File

The document contains code to record an audio signal from a microphone, write it to a .wav file, load the audio file, and apply effects like distortion and echo. The code samples audio at 8000 Hz, records for 5 seconds, heavily distorts the signal by 10x, and adds an echo with 0.5 second delay and 0.5 decay factor by delaying and adding decayed samples. Plots and audio files are generated before and after applying the effects.

Uploaded by

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

DSP File

The document contains code to record an audio signal from a microphone, write it to a .wav file, load the audio file, and apply effects like distortion and echo. The code samples audio at 8000 Hz, records for 5 seconds, heavily distorts the signal by 10x, and adds an echo with 0.5 second delay and 0.5 decay factor by delaying and adding decayed samples. Plots and audio files are generated before and after applying the effects.

Uploaded by

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

Name: Clark Amerson N.

Malqued
Section: COE 4A
Subject: Digital Signal Sampling

Code:

clc
clear all
close all
warning off
Fs=8000;%Sampling frequency in hertz
ch=1;%Number of channels--2 options--1 (mono) or 2 (stereo)
datatype='uint8';
nbits=16;%8,16,or 24
Nseconds=5;
% to record audio data from an input device ...
...such as a microphone for processing in MATLAB
recorder=audiorecorder(Fs,nbits,ch);
disp('Start speaking..')
%Record audio to audiorecorder object,...
...hold control until recording completeste
recordblocking(recorder,Nseconds);
disp('End of Recording.');
%Store recorded audio signal in numeric array
x=getaudiodata(recorder,datatype);
%Write audio file
audiowrite('test.wav',x,Fs);

% Load the audio file


[y, fs] = audioread('test.wav');
% Create a time axis
t = (0:length(y)-1) / fs;
% Plot the signal
figure;
plot(t, y);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Audio Signal');
grid on;
xlim([0 max(t)]);
ylim([-1 1]); % Adjust as needed depending on the signal range
Name: Clark Amerson N. Malqued
Section: COE 4A
Subject: Digital Signal Sampling

% Load the audio file


[y, fs] = audioread('test.wav');
% Define the distortion amount (higher values create more distortion)
distortion_amount = 10; % Adjust as needed for heavy distortion
% Apply hard clipping distortion
y_distorted = sign(y) .* min(1, abs(y * distortion_amount));
% Normalize the output to prevent clipping
y_distorted = y_distorted / max(abs(y_distorted));
% Save the heavily distorted audio
audiowrite('heavily_distorted_audio.wav', y_distorted, fs);

% Load the audio file


[inputSignal, fs] = audioread('test.wav');
% Define the parameters for the echo effect
delay = 0.5; % Delay in seconds
decay = 0.5; % Decay factor (controls how quickly the echo fades)
% Calculate the number of samples to delay
delaySamples = round(delay * fs);
% Create an empty array to store the output signal
outputSignal = zeros(size(inputSignal));
% Apply the echo effect
for i = delaySamples+1:length(inputSignal)
outputSignal(i) = inputSignal(i) + decay * inputSignal(i - delaySamples);
end
% Normalize the output signal to prevent clipping
outputSignal = outputSignal / max(abs(outputSignal));
% Save the output to a new file
audiowrite('output_with_echo.wav', outputSignal, fs);

normal signal
Name: Clark Amerson N. Malqued
Section: COE 4A
Subject: Digital Signal Sampling

Signal with echo

Distorted signal

You might also like