How to average frequency and a frequency over time graph all in Matlab?
Hello everyone,
I am encountering an issue with calculating the average frequency in MATLAB. The average frequency result is unexpectedly high and varies with each execution, even though the audio input remains unchanged. Conversely, using the same audio in Python yields more consistent and reasonable results.
If anyone can identify the problem and help fix my MATLAB code, it would be greatly appreciated. Below are two versions of the code: the first records sound directly in MATLAB, and the second processes an existing audio file.
Thank you for your assistance.
Best regards,
UMO
Code 1: also can’t figure out freq over time graph
clc
clear
warning off
Fs = 8000; % Sampling frequency in hertz
ch = 1; % Number of channels–2 options–>1 (mono) or 2 (stereo)
datatype = ‘uint8’;
nbits = 16; % 8, 16, or 24
Nseconds = 5;
% To record audio data from an input device …
% … such as a microphone for processing in MATLAB
recorder1 = audiorecorder(Fs, nbits, ch, 1); % Use device ID 1 for first microphone
% Record audio to audiorecorder object,…
% … hold control until recording completes
recordblocking(recorder1, Nseconds);
disp(‘End of Recording.’);
% Store recorded audio
x1 = getaudiodata(recorder1);
% Write audio file
audiowrite(‘test1.wav’, x1, Fs);
% Calculate time vector
t1 = 0:1/Fs:(length(x1)-1)/Fs;
% Plot time domain signal
figure;
subplot(2, 1, 1);
plot(t1, x1, ‘LineWidth’, 1.5);
xlabel(‘Time (sec)’);
ylabel(‘Amplitude’);
title(‘Time Domain Plot of the Recorded Signal’);
% Stat Information
maxValue = max(x1);
minValue = min(x1);
meanValue = mean(x1);
stdValue = std(x1);
rmsValue = rms(x1); % root mean square
% Display statistical information
disp(‘Statistical Information:’)
disp([‘Max Value: ‘, num2str(maxValue)])
disp([‘Min Value: ‘, num2str(minValue)])
disp([‘Mean Value: ‘, num2str(meanValue)])
disp([‘Standard Deviation: ‘, num2str(stdValue)])
disp([‘RMS Value: ‘, num2str(rmsValue)])
% Plot time domain signal
%figure;
% subplot(2, 1, 2);
%plot(t2, x2, ‘LineWidth’, 1.5);
%xlabel(‘Time (sec)’);
%ylabel(‘Amplitude’);
%title(‘Time Domain Plot of the Recorded Signal mirco 2’);
% Calculate FFt
n1 = length(x1);
Y1 = fft(x1);
F1 = Fs/n1*(0:n1-1);
% Find the average frequency
freq = meanfreq( x1 ,Fs );
disp([‘Average frequency of the recorded sound: ‘, num2str(freq), ‘ Hz’]);
% Plot frequency domain signal
% subplot(2, 1, 2);
figure;
plot(F1, abs(Y1(:,1)), ‘LineWidth’, 1.5);
xlabel(‘Frequency (Hz)’);
ylabel(‘Amplitude’);
title(‘Frequency Domain Plot of the Audio Signal’);
Code 2:
clc
clear
close all
warning off
% Reading input signal
[file, path] = uigetfile(‘*.wav’, ‘Select a WAV file’);
filename = fullfile(path, file);
[input_signal, Fs] = audioread(filename);
% Create time vector
t = (0:length(input_signal)-1) / Fs;
% Plotting time-domain signal
figure;
plot(t, input_signal);
grid on
title(‘Time Domain Signal’, ‘FontName’, ‘Times New Roman’, ‘FontSize’, 14);
xlabel(‘Time (sec)’, ‘FontName’, ‘Times New Roman’, ‘FontSize’, 12);
ylabel(‘Amplitude’, ‘FontName’, ‘Times New Roman’, ‘FontSize’, 12);
% Stat Information
maxValue = max(input_signal);
minValue = min(input_signal);
meanValue = mean(input_signal);
stdValue = std(input_signal);
rmsValue = rms(input_signal); % root mean square
% Display statistical information
disp(‘Statistical Information:’)
disp([‘Max Value: ‘, num2str(maxValue)])
disp([‘Min Value: ‘, num2str(minValue)])
disp([‘Mean Value: ‘, num2str(meanValue)])
disp([‘Standard Deviation: ‘, num2str(stdValue)])
disp([‘RMS Value: ‘, num2str(rmsValue)])
% Compute FFT
N = length(input_signal);
v = fft(input_signal);
v = v(1:N/2+1); % Keep only the positive frequencies
f = (0:N/2) * Fs / N; % Frequency vector
% Find the average frequency
freq = meanfreq(input_signal, Fs);
disp([‘Average frequency of the recorded sound: ‘, num2str(freq), ‘ Hz’]);
% find avg frequency manually
tfreq = sum(abs(input_signal).^2);
% Display the result
disp([‘Total frequency domain energy: ‘, num2str(tfreq)])Hello everyone,
I am encountering an issue with calculating the average frequency in MATLAB. The average frequency result is unexpectedly high and varies with each execution, even though the audio input remains unchanged. Conversely, using the same audio in Python yields more consistent and reasonable results.
If anyone can identify the problem and help fix my MATLAB code, it would be greatly appreciated. Below are two versions of the code: the first records sound directly in MATLAB, and the second processes an existing audio file.
Thank you for your assistance.
Best regards,
UMO
Code 1: also can’t figure out freq over time graph
clc
clear
warning off
Fs = 8000; % Sampling frequency in hertz
ch = 1; % Number of channels–2 options–>1 (mono) or 2 (stereo)
datatype = ‘uint8’;
nbits = 16; % 8, 16, or 24
Nseconds = 5;
% To record audio data from an input device …
% … such as a microphone for processing in MATLAB
recorder1 = audiorecorder(Fs, nbits, ch, 1); % Use device ID 1 for first microphone
% Record audio to audiorecorder object,…
% … hold control until recording completes
recordblocking(recorder1, Nseconds);
disp(‘End of Recording.’);
% Store recorded audio
x1 = getaudiodata(recorder1);
% Write audio file
audiowrite(‘test1.wav’, x1, Fs);
% Calculate time vector
t1 = 0:1/Fs:(length(x1)-1)/Fs;
% Plot time domain signal
figure;
subplot(2, 1, 1);
plot(t1, x1, ‘LineWidth’, 1.5);
xlabel(‘Time (sec)’);
ylabel(‘Amplitude’);
title(‘Time Domain Plot of the Recorded Signal’);
% Stat Information
maxValue = max(x1);
minValue = min(x1);
meanValue = mean(x1);
stdValue = std(x1);
rmsValue = rms(x1); % root mean square
% Display statistical information
disp(‘Statistical Information:’)
disp([‘Max Value: ‘, num2str(maxValue)])
disp([‘Min Value: ‘, num2str(minValue)])
disp([‘Mean Value: ‘, num2str(meanValue)])
disp([‘Standard Deviation: ‘, num2str(stdValue)])
disp([‘RMS Value: ‘, num2str(rmsValue)])
% Plot time domain signal
%figure;
% subplot(2, 1, 2);
%plot(t2, x2, ‘LineWidth’, 1.5);
%xlabel(‘Time (sec)’);
%ylabel(‘Amplitude’);
%title(‘Time Domain Plot of the Recorded Signal mirco 2’);
% Calculate FFt
n1 = length(x1);
Y1 = fft(x1);
F1 = Fs/n1*(0:n1-1);
% Find the average frequency
freq = meanfreq( x1 ,Fs );
disp([‘Average frequency of the recorded sound: ‘, num2str(freq), ‘ Hz’]);
% Plot frequency domain signal
% subplot(2, 1, 2);
figure;
plot(F1, abs(Y1(:,1)), ‘LineWidth’, 1.5);
xlabel(‘Frequency (Hz)’);
ylabel(‘Amplitude’);
title(‘Frequency Domain Plot of the Audio Signal’);
Code 2:
clc
clear
close all
warning off
% Reading input signal
[file, path] = uigetfile(‘*.wav’, ‘Select a WAV file’);
filename = fullfile(path, file);
[input_signal, Fs] = audioread(filename);
% Create time vector
t = (0:length(input_signal)-1) / Fs;
% Plotting time-domain signal
figure;
plot(t, input_signal);
grid on
title(‘Time Domain Signal’, ‘FontName’, ‘Times New Roman’, ‘FontSize’, 14);
xlabel(‘Time (sec)’, ‘FontName’, ‘Times New Roman’, ‘FontSize’, 12);
ylabel(‘Amplitude’, ‘FontName’, ‘Times New Roman’, ‘FontSize’, 12);
% Stat Information
maxValue = max(input_signal);
minValue = min(input_signal);
meanValue = mean(input_signal);
stdValue = std(input_signal);
rmsValue = rms(input_signal); % root mean square
% Display statistical information
disp(‘Statistical Information:’)
disp([‘Max Value: ‘, num2str(maxValue)])
disp([‘Min Value: ‘, num2str(minValue)])
disp([‘Mean Value: ‘, num2str(meanValue)])
disp([‘Standard Deviation: ‘, num2str(stdValue)])
disp([‘RMS Value: ‘, num2str(rmsValue)])
% Compute FFT
N = length(input_signal);
v = fft(input_signal);
v = v(1:N/2+1); % Keep only the positive frequencies
f = (0:N/2) * Fs / N; % Frequency vector
% Find the average frequency
freq = meanfreq(input_signal, Fs);
disp([‘Average frequency of the recorded sound: ‘, num2str(freq), ‘ Hz’]);
% find avg frequency manually
tfreq = sum(abs(input_signal).^2);
% Display the result
disp([‘Total frequency domain energy: ‘, num2str(tfreq)]) Hello everyone,
I am encountering an issue with calculating the average frequency in MATLAB. The average frequency result is unexpectedly high and varies with each execution, even though the audio input remains unchanged. Conversely, using the same audio in Python yields more consistent and reasonable results.
If anyone can identify the problem and help fix my MATLAB code, it would be greatly appreciated. Below are two versions of the code: the first records sound directly in MATLAB, and the second processes an existing audio file.
Thank you for your assistance.
Best regards,
UMO
Code 1: also can’t figure out freq over time graph
clc
clear
warning off
Fs = 8000; % Sampling frequency in hertz
ch = 1; % Number of channels–2 options–>1 (mono) or 2 (stereo)
datatype = ‘uint8’;
nbits = 16; % 8, 16, or 24
Nseconds = 5;
% To record audio data from an input device …
% … such as a microphone for processing in MATLAB
recorder1 = audiorecorder(Fs, nbits, ch, 1); % Use device ID 1 for first microphone
% Record audio to audiorecorder object,…
% … hold control until recording completes
recordblocking(recorder1, Nseconds);
disp(‘End of Recording.’);
% Store recorded audio
x1 = getaudiodata(recorder1);
% Write audio file
audiowrite(‘test1.wav’, x1, Fs);
% Calculate time vector
t1 = 0:1/Fs:(length(x1)-1)/Fs;
% Plot time domain signal
figure;
subplot(2, 1, 1);
plot(t1, x1, ‘LineWidth’, 1.5);
xlabel(‘Time (sec)’);
ylabel(‘Amplitude’);
title(‘Time Domain Plot of the Recorded Signal’);
% Stat Information
maxValue = max(x1);
minValue = min(x1);
meanValue = mean(x1);
stdValue = std(x1);
rmsValue = rms(x1); % root mean square
% Display statistical information
disp(‘Statistical Information:’)
disp([‘Max Value: ‘, num2str(maxValue)])
disp([‘Min Value: ‘, num2str(minValue)])
disp([‘Mean Value: ‘, num2str(meanValue)])
disp([‘Standard Deviation: ‘, num2str(stdValue)])
disp([‘RMS Value: ‘, num2str(rmsValue)])
% Plot time domain signal
%figure;
% subplot(2, 1, 2);
%plot(t2, x2, ‘LineWidth’, 1.5);
%xlabel(‘Time (sec)’);
%ylabel(‘Amplitude’);
%title(‘Time Domain Plot of the Recorded Signal mirco 2’);
% Calculate FFt
n1 = length(x1);
Y1 = fft(x1);
F1 = Fs/n1*(0:n1-1);
% Find the average frequency
freq = meanfreq( x1 ,Fs );
disp([‘Average frequency of the recorded sound: ‘, num2str(freq), ‘ Hz’]);
% Plot frequency domain signal
% subplot(2, 1, 2);
figure;
plot(F1, abs(Y1(:,1)), ‘LineWidth’, 1.5);
xlabel(‘Frequency (Hz)’);
ylabel(‘Amplitude’);
title(‘Frequency Domain Plot of the Audio Signal’);
Code 2:
clc
clear
close all
warning off
% Reading input signal
[file, path] = uigetfile(‘*.wav’, ‘Select a WAV file’);
filename = fullfile(path, file);
[input_signal, Fs] = audioread(filename);
% Create time vector
t = (0:length(input_signal)-1) / Fs;
% Plotting time-domain signal
figure;
plot(t, input_signal);
grid on
title(‘Time Domain Signal’, ‘FontName’, ‘Times New Roman’, ‘FontSize’, 14);
xlabel(‘Time (sec)’, ‘FontName’, ‘Times New Roman’, ‘FontSize’, 12);
ylabel(‘Amplitude’, ‘FontName’, ‘Times New Roman’, ‘FontSize’, 12);
% Stat Information
maxValue = max(input_signal);
minValue = min(input_signal);
meanValue = mean(input_signal);
stdValue = std(input_signal);
rmsValue = rms(input_signal); % root mean square
% Display statistical information
disp(‘Statistical Information:’)
disp([‘Max Value: ‘, num2str(maxValue)])
disp([‘Min Value: ‘, num2str(minValue)])
disp([‘Mean Value: ‘, num2str(meanValue)])
disp([‘Standard Deviation: ‘, num2str(stdValue)])
disp([‘RMS Value: ‘, num2str(rmsValue)])
% Compute FFT
N = length(input_signal);
v = fft(input_signal);
v = v(1:N/2+1); % Keep only the positive frequencies
f = (0:N/2) * Fs / N; % Frequency vector
% Find the average frequency
freq = meanfreq(input_signal, Fs);
disp([‘Average frequency of the recorded sound: ‘, num2str(freq), ‘ Hz’]);
% find avg frequency manually
tfreq = sum(abs(input_signal).^2);
% Display the result
disp([‘Total frequency domain energy: ‘, num2str(tfreq)]) frequency, meanfreq, meanfrequency, frequencyovertime, graph, help MATLAB Answers — New Questions