How to calculate average frequency and a frequency over time graph ... (2024)

62 Ansichten (letzte 30 Tage)

Ältere Kommentare anzeigen

Ugonne am 27 Jun. 2024 um 21:36

  • Verknüpfen

    Direkter Link zu dieser Frage

    https://de.mathworks.com/matlabcentral/answers/2132696-how-to-calculate-average-frequency-and-a-frequency-over-time-graph-all-in-matlab

  • Verknüpfen

    Direkter Link zu dieser Frage

    https://de.mathworks.com/matlabcentral/answers/2132696-how-to-calculate-average-frequency-and-a-frequency-over-time-graph-all-in-matlab

Bearbeitet: Ugonne am 5 Jul. 2024 um 23:16

In MATLAB Online öffnen

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

No audio input device found on this system.

% Record audio to audiorecorder object,...

% ... hold control until recording completes

recordblocking(recorder1, Nseconds);

disp('End of Recording.');

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)])

2 Kommentare

Keine anzeigenKeine ausblenden

Star Strider am 27 Jun. 2024 um 23:28

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/2132696-how-to-calculate-average-frequency-and-a-frequency-over-time-graph-all-in-matlab#comment_3197541

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/2132696-how-to-calculate-average-frequency-and-a-frequency-over-time-graph-all-in-matlab#comment_3197541

If you have the Signal Processing Toolbox, use the meanfreq function.

Ugonne am 28 Jun. 2024 um 15:24

Direkter Link zu diesem Kommentar

https://de.mathworks.com/matlabcentral/answers/2132696-how-to-calculate-average-frequency-and-a-frequency-over-time-graph-all-in-matlab#comment_3198026

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.mathworks.com/matlabcentral/answers/2132696-how-to-calculate-average-frequency-and-a-frequency-over-time-graph-all-in-matlab#comment_3198026

Yes I used that install. So I can use meanfreq.

Melden Sie sich an, um zu kommentieren.

Melden Sie sich an, um diese Frage zu beantworten.

Antworten (0)

Melden Sie sich an, um diese Frage zu beantworten.

Siehe auch

Kategorien

Signal ProcessingAudio ToolboxSimulation, Tuning, and Visualization

Mehr zu Simulation, Tuning, and Visualization finden Sie in Help Center und File Exchange

Tags

  • frequency
  • meanfreq
  • meanfrequency
  • frequencyovertime
  • graph

Produkte

  • MATLAB

Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Es ist ein Fehler aufgetreten

Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.


Translated by How to calculate average frequency and a frequency over time graph ... (4)

How to calculate average frequency and a frequency over time graph ... (5)

Website auswählen

Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .

Sie können auch eine Website aus der folgenden Liste auswählen:

Amerika

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europa

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asien-Pazifik

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Kontakt zu Ihrer lokalen Niederlassung

How to calculate average frequency and a frequency over time graph ... (2024)
Top Articles
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 6638

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.