create a script that takes the fft and separates the noise from the harmonics in matlab

To separate noise from harmonics in a signal using FFT in MATLAB, you can use a technique called spectral subtraction.

Here is an example script that demonstrates how to do that:

main.m
% Load the signal
[x, fs] = audioread('signal.wav');

% Define the frequency range of the harmonics
harmonics_range = [1800, 2000];

% Compute the FFT of the signal
X = fft(x);

% Compute the power spectrum
P = abs(X).^2;

% Compute the noise spectrum by averaging the power spectrum outside the harmonics range
P_noise = mean([P(1:harmonics_range(1))' P(harmonics_range(2):end)'], 2);

% Compute the spectral subtraction function
H = max(P - P_noise, 0);

% Compute the enhanced signal by inverse FFT
y = ifft(X .* sqrt(H ./ P));

% Save the enhanced signal
audiowrite('enhanced.wav', y, fs);
594 chars
24 lines

In this script, we first load the input signal (assuming that it is in WAV format), and define the frequency range of the harmonics that we want to keep (in this case, we keep the harmonics between 1800 and 2000 Hz).

Next, we compute the FFT of the signal and its power spectrum. We also compute the noise spectrum by averaging the power spectrum outside the harmonics range.

Then, we compute the spectral subtraction function by subtracting the noise spectrum from the power spectrum and taking the maximum between the result and zero (to eliminate negative values).

Finally, we compute the enhanced signal by multiplying the input signal's FFT by the square root of the spectral subtraction function divided by the power spectrum, and take the inverse FFT to get the time-domain signal. The enhanced signal is saved in a WAV file.

Note that this is just an example, and you may need to adjust the parameters and apply additional techniques based on your specific requirements and characteristics of the signal.

related categories

gistlibby LogSnag