Borrar filtros
Borrar filtros

How I get the frequencies in Hz of a specific signal?

12 visualizaciones (últimos 30 días)
Carlos Daniel
Carlos Daniel el 14 de Ag. de 2023
Respondida: Les Beckham el 16 de Ag. de 2023
I created a matlab code that gives me in output a signal that starting at 80 Hz go up till 800Hz with the following code
% % This code will generate a saltatory stimulus of 10 Hz up to 800 Hrz (the stimulus was frequency filtered in audacity to exclude frequencies bellow 80 and above 1000Hz)
duration = 80;
freq_step = 10;
sampling_rate = 44100;
% Generate the time samples
t = linspace(0, duration, sampling_rate * duration);
%generation of the audiowave with the increasing frequency steps
freq_range = freq_step * (0:length(t)-1) / sampling_rate;
audio_waveform = 0.5*sin(pi * freq_range.* t);
audio_waveform(1:(8*sampling_rate)) = [];
%normalization of the audiowave form
%audio_waveform = audio_waveform / max(abs(audio_waveform)); %this
%commented
%eliminate the normalization of the wave form
%create the audioplayer object
player = audioplayer(audio_waveform, sampling_rate);
%play(player);
%save the file as WAV file format
audiowrite("output_sound1.wav", audio_waveform, sampling_rate);
Now my real gole is to given a signal as an input I would like to obtain the frequencies that my audio has allong the whole duration..but i dont know how to get the frequencies ... I thought that by using a STFT i will get it and i used my signal (which i know should have frequencies from 80 upt till 800 with an increas of 10Hz in every step) but I cannot get the array and spectrogram or something else that should tell me the frequency components of my signal allong the duration something like 1s = frequency 80 Hz, 2s = frequency 90 Hz and so on till 80s = frequency 800Hz. what is wrong with my code?
filename = 'output_sound1.wav'; % Replace wih the actual file name
[audio_data, sample_rate] = audioread(filename);
% now i need to calculate the number of samples in mz audio data
num_samples = length(audio_data);
%calculate the frequencies in the file this with the FFT
frequencies = (0:num_samples-1)* sample_rate / num_samples;
% Initialize arrays to store dB and volt values
amplitude_dB = zeros(length(frequencies), 1);
amplitude_volt = zeros(length(frequencies), 1);
for i = 1:length(frequencies)
freq_index = (i);
% Calculate amplitude in dB and volts78
amplitude_dB(i) = 20 * log10(abs(audio_data(freq_index)));
amplitude_volt(i) = abs(audio_data(freq_index));
end
% Create a table
Frequency_Hz = frequencies';
frequency_table = table(Frequency_Hz, amplitude_dB, amplitude_volt);
% lets try doing the spectrogram of my signal
y_stft = stft(audio_data);
N = length(audio_data);
y = audio_data-mean(audio_data);
fr= sample_rate * (0:floor((1/2)/N));
s = abs(y_stft(1:floor(N/2+1)));
%plt the single-sided magnitide spectrum of the signal
% figure
plot(fr,s);
title("single sided spectrum of my signal");
xlabel("frequenzy");
ylabel("SFTF of my signal");
window = 100;
overlap = window/2;
nfft= 1024;
[S, f, t] = spectrogram (audio_data, window, overlap, nfft, N);
%
% %conversion of the spectrogram in dB air scale
%
S_dB = 10*log10(abs(10));
% %plot the spectrogram
%
figure
imagesc (t, f, S_dB);
axis xy;
xlabel("Time (s)");
ylabel("Frequency(Hz)");
title("Spectrogram of my generated signal");
colorbar;

Respuestas (1)

Les Beckham
Les Beckham el 16 de Ag. de 2023
I think this is close to what you are looking for.
% % This code will generate a saltatory stimulus of 10 Hz up to 800 Hrz (the stimulus was frequency filtered in audacity to exclude frequencies bellow 80 and above 1000Hz)
duration = 80;
freq_step = 10;
sampling_rate = 44100;
% Generate the time samples
t = linspace(0, duration, sampling_rate * duration);
%generation of the audiowave with the increasing frequency steps
freq_range = freq_step * (0:length(t)-1) / sampling_rate;
audio_waveform = 0.5*sin(pi * freq_range.* t);
audio_waveform(1:(8*sampling_rate)) = [];
%normalization of the audiowave form
%audio_waveform = audio_waveform / max(abs(audio_waveform)); %this
%commented
%eliminate the normalization of the wave form
%create the audioplayer object
% player = audioplayer(audio_waveform, sampling_rate);
%play(player);
%save the file as WAV file format
% audiowrite("output_sound1.wav", audio_waveform, sampling_rate);
m = islocalmax(audio_waveform); % find indexes of the signal peaks
t_plot = t; % make a copy of the time vector to use for plotting
t_plot(1:(8*sampling_rate)) = []; % truncate the time vector the same way the audio_waveform was truncated
f = 1 ./ diff(t_plot(m)); % frequency is inverse of time between signal peaks
t_plot = t_plot(m); % extract the time at each of the peaks
% whos
plot(t_plot(2:end), f); % skip first time point since diff reduces vector length by one
grid on
xlabel('Time (sec)')
ylabel('Frequency (Hz)')

Categorías

Más información sobre Spectral Measurements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by