write function that generates an echo to audio.Why am i getting this error
30 次查看(过去 30 天)
显示 更早的评论
function output = echo_gen(input, fs, delay, amp);
% Load the 'gong' sound
% Define the range of samples you want to read from the original audio
% startSample = 3;
% endSample = 5 * Fs;
% Create a delay effect by shifting the audio signal
delaySamples = round(delay.* fs); % Adjust the delay time as needed
delayedSignal = [zeros(delaySamples, 1); input];
% % Determine the length of the combined audio
combinedLength = max(length(input), length(delayedSignal));
%
% % Pad the delayed signal if needed
% if length(delayedSignal) < combinedLength
% delayedSignal = [delayedSignal; zeros(combinedLength - length(delayedSignal), 1)];
% end
%
% Pad the original signal if needed
if length(input) < combinedLength
input = [input; zeros(combinedLength - length(input), 1)];
end
% Combine the audio signals
output1 = input + delayedSignal;
%for amplification
output=amp.*output1;
% Normalize the combined audio to prevent clipping
output = output / max(abs(output));
% Write the combined audio to a new file
audiowrite('combined_audio.wav', output, fs);
% Play the combined audio
sound(output, fs);
end

0 个评论
回答(1 个)
Mathieu NOE
2023-8-30
hello
you code can be simplified . Also an echoed signal means you add to the original a fraction (in amplitude) of the delayed version of it. The amount (amplitude) of the delayed signal is given by the factor "amp" , now correctly used in this code :
%%%%%%%%%%%%%
% Load the 'gong' sound
load gong.mat
input = y;
fs = Fs;
%%%%%%%%%%%%%
delay = 0.1;
amp = 0.1;
% Define the range of samples you want to read from the original audio
% startSample = 3;
% endSample = 5 * Fs;
% Create a delay effect by shifting the audio signal
delaySamples = round(delay.* fs); % Adjust the delay time as needed
delayedSignal = [zeros(delaySamples, 1); input];
% Pad the original signal
input = [input; zeros(delaySamples, 1)];
% Combine the audio signals
output = input + amp*delayedSignal; % correction here
% Normalize the combined audio to prevent clipping
output = output / max(abs(output));
% Write the combined audio to a new file
audiowrite('combined_audio.wav', output, fs);
% Play the combined audio
sound(output, fs);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!