getting alot of exceeding memory errors and errors such as array exceeding maximum allowed size in my code

2 views (last 30 days)
So basically I am trying to record a sentence from my microphone, then adding a noise in the form of a sinusoidal to it and then getting the fourier of this, playing it back and then filtering out the noise to obtain as close to the original sentence as possible but it keeps giving me memory related errors especially when adding the noise to the original signal. Since I am not well versed with MATLAB and how to reduce the array size while retaining the results required, could please help me out with the code provided. THANK YOU!!
% Set the duration of the recording in seconds
duration = 5;
% Record a sentence from the microphone
recorder = audiorecorder(44100, 16, 1);
disp('Say a sentence...')
recordblocking(recorder, duration);
% Play back the recorded sentence
disp('Playing back the recorded sentence...')
play(recorder);
% Get the recorded audio data
audio_data = getaudiodata(recorder);
% Add a sine wave with a frequency of 10 kHz to the audio data
noise = sin(2*pi*10000*(1:length(audio_data))/44100);
noisy_audio_data = audio_data + noise;
% Plot the noisy audio data in the time domain
figure
plot(noisy_audio_data)
title('Noisy audio data in the time domain')
xlabel('Sample index')
ylabel('Amplitude')
% Compute the Fast Fourier Transform (FFT) of the noisy audio data
fft_data = fft(noisy_audio_data);
% Plot the frequency spectrum of the noisy audio data
figure
plot(abs(fft_data))
title('Frequency spectrum of the noisy audio data')
xlabel('Frequency')
ylabel('Amplitude')
% Filter out the noise by applying a low-pass filter with a cut-off frequency of 9 kHz
cutoff = 9e3;
[b, a] = butter(6, cutoff/(44100/2), 'low');
filtered_audio_data = filter(b, a, noisy_audio_data);
% Play back the filtered audio data
disp('Playing back the filtered audio data...')
sound(filtered_audio_data, 44100);
% Plot the filtered audio data in the time domain
figure
plot(filtered_audio_data)
title('Filtered audio data in the time domain')
xlabel('Sample index')
ylabel('Amplitude')
% Compute the FFT of the filtered audio data
fft_data = fft(filtered_audio_data);
% Plot the frequency spectrum of the filtered audio data
figure
plot(abs(fft_data))
title('Frequency spectrum of the filtered audio data')
xlabel('Frequency')
ylabel('Amplitude')

Accepted Answer

Walter Roberson
Walter Roberson on 7 Dec 2022
record audio returns rows of data. Your sine wave is columns

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!