-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_audio_cleaning.m
More file actions
51 lines (39 loc) · 1.55 KB
/
Copy pathai_audio_cleaning.m
File metadata and controls
51 lines (39 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
% Load clean audio
[cleanAudio, Fs] = audioread('non_noise.wav');
cleanAudio = cleanAudio(:, 1); % Force to mono / first channel if stereo
% Load noisy audio
[noisyAudio, ~] = audioread('noisy_music.wav');
noisyAudio = noisyAudio(:, 1); % Force to mono / first channel if stereo
% Match lengths strictly
minLength = min(length(cleanAudio), length(noisyAudio));
cleanAudio = cleanAudio(1:minLength);
noisyAudio = noisyAudio(1:minLength);
% Ensure they are vertical column vectors
cleanAudio = cleanAudio(:);
noisyAudio = noisyAudio(:);
% Pre-filter noisy audio (CRITICAL: Match workshop live-demo pipeline)
noisyAudio = lowpass(noisyAudio, 4000, Fs);
% Create features for AI (Must be exact same length column vectors)
X = [
noisyAudio, ...
movmean(noisyAudio, 5), ...
movmean(noisyAudio, 20), ...
movmean(noisyAudio, 50), ...
movmean(noisyAudio, 100), ...
movmean(noisyAudio, 200)
];
% Normalize the feature matrix to stabilize fitlm tracking
X = normalize(X);
% Train Linear Regression Model
model = fitlm(X, cleanAudio); % This will now execute perfectly
% Predict cleaned audio
predictedAudio = predict(model, X); % asks model to predict cleaner audio based on what it learned
% Final smoothing step
predictedAudio = movmean(predictedAudio, 8); % applies smoothing after predicting audio
% Normalize output audio
predictedAudio = predictedAudio / max(abs(predictedAudio));
% Save audio
audiowrite('ai_filtered.wav', predictedAudio, Fs); % saves audio to another file name
% Play result
disp('Playing AI Cleaned Audio...')
sound(predictedAudio, Fs)