# Como fazer gráfico de frequência fft

3 views (last 30 days)
Márcio Silva on 30 Aug 2022
Commented: Mathieu NOE on 12 Sep 2022
Good Morning! I'm trying to plot the fft graph, but I have to set the frequency, but I'm not getting it.
close all
clear all
clc;
M = length (x); % comprimento do sinal
p = (1-0.67); % sobreposição
% Divisão de blocos
n = 5; % número de blocos
N = fix (M/(1 + (n-1)*p)); % comprimento do bloco
m1 = 1;
m2 = N;
z2 = 0;
for v = 1:n
x1 = x(m1:m2); % um pedaço
Z1 = fft(x1); %transformada de Fourier
z2 = z2 + conj(Z1); %conjugado de z1
m1 = m1 + fix(p*N); % inicio
m2 = m1 + N-1; %final
end
% Módulo da Transformada de Fourier
z = abs(z2);
plot(abs(z2));
%title('Espectro do sinal(Domínio da frequência)')
%ylabel('Amplitude');
%xlabel('Frequência (Hz)');

Mathieu NOE on 31 Aug 2022
hello
we cannot test your code as you don't provide the mat file (use the paperclip button)
otherwise I can suggest you to look at my code below and test it (you probably have a wav file somewhere in your PC). The adaptation to a mat file should be easy
hope it helps !
clc
clearvars
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FFT parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NFFT = 2000; % then df = 1 Hz
OVERLAP = 0.75;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dt = 1/Fs;
[samples,channels] = size(signal);
% time vector
time = (0:samples-1)*dt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display 1 : time domain plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(1),
plot(time,signal);grid on
title(['Time plot / Fs = ' num2str(Fs) ' Hz ']);
xlabel('Time (s)');ylabel('Amplitude');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display 2 : averaged FFT spectrum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[freq, spectrum_raw] = myfft_peak(signal,Fs,NFFT,OVERLAP);
figure(2),plot(freq,20*log10(spectrum_raw));grid on
df = freq(2)-freq(1); % frequency resolution
title(['Averaged FFT Spectrum / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(df,3) ' Hz ']);
xlabel('Frequency (Hz)');ylabel('Amplitude (dB)');
function [freq_vector,fft_spectrum] = myfft_peak(signal, Fs, nfft, Overlap)
% FFT peak spectrum of signal (example sinus amplitude 1 = 0 dB after fft).
% Linear averaging
% signal - input signal,
% Fs - Sampling frequency (Hz).
% nfft - FFT window size
% Overlap - buffer percentage of overlap % (between 0 and 0.95)
[samples,channels] = size(signal);
% fill signal with zeros if its length is lower than nfft
if samples<nfft
s_tmp = zeros(nfft,channels);
s_tmp((1:samples),:) = signal;
signal = s_tmp;
samples = nfft;
end
% window : hanning
window = hanning(nfft);
window = window(:);
% compute fft with overlap
offset = fix((1-Overlap)*nfft);
spectnum = 1+ fix((samples-nfft)/offset); % Number of windows
% % for info is equivalent to :
% noverlap = Overlap*nfft;
% spectnum = fix((samples-noverlap)/(nfft-noverlap)); % Number of windows
% main loop
fft_spectrum = 0;
for i=1:spectnum
start = (i-1)*offset;
sw = signal((1+start):(start+nfft),:).*(window*ones(1,channels));
fft_spectrum = fft_spectrum + (abs(fft(sw))*4/nfft); % X=fft(x.*hanning(N))*4/N; % hanning only
end
fft_spectrum = fft_spectrum/spectnum; % to do linear averaging scaling
% one sidded fft spectrum % Select first half
if rem(nfft,2) % nfft odd
select = (1:(nfft+1)/2)';
else
select = (1:nfft/2+1)';
end
fft_spectrum = fft_spectrum(select,:);
freq_vector = (select - 1)*Fs/nfft;
end
Mathieu NOE on 12 Sep 2022
hello again
problem solved ?