How to trim, the extra audio portion after audio processing in two channel ?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi ,
i have a problem in audio processing . I have trasmitted audio in one hardware did some processing and reconstructed the audio in receiver . But during recostruiction some exctra rows got added in the both the channels , the adiitions are different in different channels . I need to remove the same for comapring . Please suggest a method .
I did it like this .
desiredFs=48000;
audio_frame_CH1_FF=0;
audio_frame_CH2_FF=0;
[x,OriginalFs] = audioread("file.mp3"); % y samples from audio with Fs sampling frequency in [Hz].
%
N = length(x); % sample lenth
duration_audio=N/OriginalFs;
tx = linspace(0, N/OriginalFs, N);
plot(tx, x);
hold on
afr = dsp.AudioFileReader('file.mp3','ReadRange',[1 8.007*desiredFs]);% value of duration_audio
framecnt=0;
while ~isDone(afr)
audio_frame = afr();
audio_frame_CH1=audio_frame(:,1);
audio_frame_CH2=audio_frame(:,2);
audio_frame_CH1_FF=[audio_frame_CH1_FF ; audio_frame_CH1];
audio_frame_CH2_FF=[audio_frame_CH1_FF ; audio_frame_CH2];
framecnt=framecnt+1;
end
release(afr);
fixedAudioFrame1=Audiotrim(x,audio_frame_CH1_FF);
fixedAudioFrame2=Audiotrim(x,audio_frame_CH2_FF);
fixedAudioFrame=[audio_frame_CH1_FF ; audio_frame_CH2_FF]
plot(fixedAudioFrame,tx);
hold off
audiowrite('processed.wav',fixedAudioFrame,desiredFs);
The code of Audiotrim is like this
function trimmedAudio=Audiotrim(x,audio_frame_CH1_FF)
slength =length(x(:,1))
elength =length(audio_frame_CH1_FF(:,1))
extraRow =elength-slength
i=1;
%for i=1:floor(extraRow/2)
while i< floor(extraRow/2)
if i== floor(extraRow/2)
i=1;
end
if (length(audio_frame_CH1_FF(:,1))==length(x(:,1)))
break;
end
audio_frame_CH1_FF(slength+i,:) = [];
end
trimmedAudio=audio_frame_CH1_FF;
end
Issue is. Due to the different rows the thsi function does not work in both the channel . I know thios method is not the right approach . Please suggest a better approach towards this problem.
0 Commenti
Risposte (1)
Abhimenyu
il 14 Apr 2024
Hi Mainak,
From the information shared, I could infer that you're trying to align the lengths of the audio frames from two channels to compare them properly. The signal processing technique of "cross-correlation" would help to align the audio frames using the time lag between them rather than manually adjusting the lengths. The initial code that loads and reads the files will remain the same as given below in the MATLAB code:
% Load your audio file (replace "file.mp3" with your actual file path)
[x, OriginalFs] = audioread("file.mp3");
% Desired sampling frequency
desiredFs = 48000;
% Read audio frames
afr = dsp.AudioFileReader('file.mp3', 'ReadRange', [1 8.007 * desiredFs]);
framecnt = 0;
audio_frame_CH1_FF = [];
audio_frame_CH2_FF = [];
while ~isDone(afr)
audio_frame = afr();
audio_frame_CH1 = audio_frame(:, 1);
audio_frame_CH2 = audio_frame(:, 2);
audio_frame_CH1_FF = [audio_frame_CH1_FF; audio_frame_CH1];
audio_frame_CH2_FF = [audio_frame_CH2_FF; audio_frame_CH2];
framecnt = framecnt + 1;
end
release(afr);
Then we use the MATLAB function "xcorr" to find the time delay between the two channels. The peak of the cross-correlation gives the point of highest similarity, indicating how much one signal is delayed relative to the other.
% Cross-correlation to find time delay (lag) between channels
[acor, lag] = xcorr(audio_frame_CH1_FF, audio_frame_CH2_FF);
[~, I] = max(abs(acor));
lagDiff = lag(I);
With the lag determined, the starting point of each channel is adjusted to align them in time. This step is crucial for ensuring that any further processing or analysis on the channels considers their correct temporal relationship.
% Align the channels
if lagDiff > 0
audio_frame_CH2_aligned = audio_frame_CH2_FF(lagDiff + 1:end);
audio_frame_CH1_aligned = audio_frame_CH1_FF(1:end - lagDiff);
else
audio_frame_CH1_aligned = audio_frame_CH1_FF(-lagDiff + 1:end);
audio_frame_CH2_aligned = audio_frame_CH2_FF(1:end + lagDiff);
end
After alignment, the signals are trimmed to the length of the shorter one to ensure they have the same duration. This is important for stereo playback and any comparative analysis.
% Trim to match lengths
minLength = min(length(audio_frame_CH1_aligned), length(audio_frame_CH2_aligned));
audio_frame_CH1_aligned = audio_frame_CH1_aligned(1:minLength);
audio_frame_CH2_aligned = audio_frame_CH2_aligned(1:minLength);
Finally, the aligned and trimmed audio is saved back to a file, ready for playback or further processing.
% Now you can compare or process the aligned channels
% For example, save the processed audio
audiowrite('processed.wav', [audio_frame_CH1_aligned, audio_frame_CH2_aligned], desiredFs);
For a better understanding of the "xcorr" function, please follow this MATLAB R2024A documentation link: https://www.mathworks.com/help/matlab/ref/xcorr.html
I hope this helps!
0 Commenti
Vedere anche
Categorie
Scopri di più su Audio I/O and Waveform Generation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!