how i can modifiy the code to make the bandwith of fft less ?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
this code of fft read from csv file and i wanna reduce the bandwight of the frequancy.
how i should do that without effect on the resluts?
this is the code :
% Read the CSV file, automatically skipping the header row
data = readmatrix('test.csv');
% Extract the time and data columns
time = data(:, 1); % Assuming the first column is time
t = data(:, 3); % Assuming the second column is data
% Sample rate
dt = time(2) - time(1); % Assuming uniform time intervals
fs = 1/dt;
% Length of the signal
L = length(t);
% Time Domain Signal
figure;
plot(time(1:5000), t(1:5000));
title('Time Domain Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Perform FFT
t_f = fft(t);
% Construct frequency vector
f = fs * (0:(L/2)) / L;
% Compute the magnitude of the FFT and keep only the positive frequencies
t_f_mag = abs(t_f/L);
t_f_mag = t_f_mag(1:(L/2)+1);
% Plot the frequency domain signal
figure;
plot(f, t_f_mag);
title('Frequency Domain Signal');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
and this code useful when I implement it in sigma delta?
0 comentarios
Respuestas (2)
Star Strider
el 12 de Ag. de 2024
Add:
xlim([0 1.5E+3])
after the plot call to distinctly see the two peaks.
Tweaked code —
% Read the CSV file, automatically skipping the header row
data = readmatrix('test.csv');
% Extract the time and data columns
time = data(:, 1); % Assuming the first column is time
t = data(:, 3); % Assuming the second column is data
% Sample rate
dt = time(2) - time(1); % Assuming uniform time intervals
fs = 1/dt;
% Length of the signal
L = length(t);
% Time Domain Signal
figure;
plot(time(1:5000), t(1:5000));
title('Time Domain Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Perform FFT
t_f = fft(t);
% Construct frequency vector
f = fs * (0:(L/2)) / L;
% Compute the magnitude of the FFT and keep only the positive frequencies
t_f_mag = abs(t_f/L);
t_f_mag = t_f_mag(1:fix((L/2))+1);
% Plot the frequency domain signal
figure;
plot(f, t_f_mag);
title('Frequency Domain Signal');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
xlim([0 1.5E+3]) % <— ADDED
[Peaks,Freqs] = findpeaks(t_f_mag, f(:), 'MinPeakProminence',0.2); % <— ADDED
Result = table(Freqs,Peaks) % <— ADDED
.
0 comentarios
William Rose
el 12 de Ag. de 2024
To get the very narrow bandwidth shown inthe second plot in your PDF, you need to inccrease the duration of the sample of the signal. The spacing between frequencies in the discrete Fourier transform is where T is the duration of the signal. The second plot in your pdf suggests a very small
value for , which requires a large signal duration.
By the way, the comments in your code say you want the seocnd column of data in the spreadsheet, but the code selects the thrid column.
You use the variable name "t" for the signal , and "time" for the time base. THat comfused me at first, since I thought "t" was time. If it were me, I would use "x" or "y" for the signal. But that is just my opinion.
When I run your code, I do not get a plot like the one you show in the PDF. I get the follwoing two plots:
I also get a warning error when I run your code:
>> sampleFFT
Warning: Integer operands are required for colon operator when used as index.
> In sampleFFT (line 28)
This error occurs because L, the length of the signal, is odd, and because you compute L/2 as you try to plot only one side of the amplitude spectrum. I changed your code to plot the full amplitude spectrum. This fixes the warning error, and the frequency plot extends up to 100 kHz.
More later.
1 comentario
William Rose
el 12 de Ag. de 2024
Editada: William Rose
el 12 de Ag. de 2024
[Edit: I posted this comment before I was done.]
Plot 1 in your PDF shows a signal with two frequency components, at 200 Hz and 1000 Hz. The spacing, , between samples in this plot is 200 Hz. This spacing is the expected spacing, since the signal duration is 0.005 s, as I explained above.
Plot 2 in your PDF shows an amplitude spectrum for a signal with frequnies of approximately 960 Hz and 1040 Hz. The frequency spacing in this plot is on the order of 1 Hz. To generate a signal with an amplitude spectrum like the second plot in your PDF:
fs=5000; % sampling frequency (Hz)
% fs is about 5 times the highest frequency component in the signal
dt=1/fs; % sampling interval (s)
df=1; % desired frequency spacing of FFT (Hz)
T=1/df; % signal duration (s), to achieve desired df
N=T/dt; % number of samples
t=(0:N-1)*dt; % time vector (s)
f1=960; f2=1040; % frequencies in the signal (Hz)
x=sin(2*pi*f1*t)+sin(2*pi*f2*t); % the signal
Compute amplitude spectrum
X=fft(x); % discrete Fourier transform of x
f=(0:N-1)*df; % vector of frequencies
Plot the signal and the amplitude spectrum
figure; subplot(211); plot(t,x,'-r'); grid on
xlabel('Time (s)'); ylabel('Ampltude'); title('x(t)')
subplot(212); plot(f,abs(X),'-r'); grid on; xlim([900 1100])
xlabel('Frequency (Hz)'); ylabel('Ampltude'); title('X(f)')
This looks like amplitude spectrum you wanted.
Ver también
Categorías
Más información sobre Spectral Measurements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!