How to find the sampling frequency?
63 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Arcadius
el 24 de Oct. de 2023
Comentada: Star Strider
el 24 de Oct. de 2023
Hello, I am trying to plot the frequency domain of a fourier transform of a piecewise signal. Can someone help me understand further the right sampling frequency as well as the frequency domain maximum/minimum value to evaluate the correct frequency domain? I appreciate your time.
% Sampling Time
ts = 1000;
% Time domain variables
t1=-2:1/ts:2;
t2=2:1/ts:4;
% Evaluation at those time domains
x1=4*ones(size(t1));
x2=-8*ones(size(t2));
% Combining both time domains of the piecewise functions
t=[t1 t2];
% Combining functions
f=[x1 x2];
% Plot the piecewise function with respect to time
figure(1);
plot(t,f);
ylim([-10 6]);
% Unsure of this part's sample and frequency domain
% I chose the sample based on the size of the combined time (t).
% Samples
N=6002;
% Frequency Domain (
freq=-ts/2:ts/(N-1):ts/2;
% Fourier transform of the piecewise function (f)
ft=fft(fftshift(f));
% Figure 2 shows the absolute of the fourier transform with respect to the
% frequency
figure(2);
plot(freq,abs(ft));
0 comentarios
Respuesta aceptada
Star Strider
el 24 de Oct. de 2023
The sampling frequency is the inverse of the sampling interval. The sampling frequency appears to be ‘ts’ .
These correspond to the MATLAB fft documentation for the frequency vector:
Fv = linspace(-Fs/2, Fs/2-Fs/length(s), length(s)); % EVEN 'length(s)' (Asymmetric)
Fv = linspace(-Fs/2, Fs/2, length(s)); % ODD 'length(s)' (Symmetric)
where ‘s’ is the signal vector (or by default, the columns of a signal matrix).
% Sampling Time
ts = 1000;
% Time domain variables
t1=-2:1/ts:2;
t2=2:1/ts:4;
% Evaluation at those time domains
x1=4*ones(size(t1));
x2=-8*ones(size(t2));
% Combining both time domains of the piecewise functions
t=[t1 t2];
% Combining functions
f=[x1 x2];
% Plot the piecewise function with respect to time
figure(1);
plot(t,f);
ylim([-10 6]);
% Unsure of this part's sample and frequency domain
% I chose the sample based on the size of the combined time (t).
% Samples
N=6002;
% Frequency Domain (
freq=-ts/2:ts/(N-1):ts/2
Fv = linspace(-ts/2, ts/2-ts/length(f), length(f)) % EVEN 'length(s)' (Asymmetric)
% Fourier transform of the piecewise function (f)
% % ft=fft(fftshift(f));
ft = fftshift(fft(f));
Size_ft = size(ft)
% Figure 2 shows the absolute of the fourier transform with respect to the
% frequency
figure(2);
plot(freq,abs(ft));
grid
grid('minor')
The only change I made to your code was to change the order of the fft and fftshift calls, since that is apparently what you intend.
Your ‘freq’ and my ‘Fv’ are slightly different because they are calculated differently, however they are essentially the same. (I did that to check. Use yours.)
.
2 comentarios
Star Strider
el 24 de Oct. de 2023
Probably the easiest way at this point would be to test for the frequency limits (-100,100) and use logical indexing to select the elemeents for the plot. Another option is simply to usethe xlim function.
Doing both —
% Sampling Time
ts = 1000;
% Time domain variables
t1=-2:1/ts:2;
t2=2:1/ts:4;
% Evaluation at those time domains
x1=4*ones(size(t1));
x2=-8*ones(size(t2));
% Combining both time domains of the piecewise functions
t=[t1 t2];
% Combining functions
f=[x1 x2];
% Plot the piecewise function with respect to time
figure(1);
plot(t,f);
ylim([-10 6]);
% Unsure of this part's sample and frequency domain
% I chose the sample based on the size of the combined time (t).
% Samples
N=6002;
% Frequency Domain (
freq=-ts/2:ts/(N-1):ts/2;
% Fv = linspace(-ts/2, ts/2-ts/length(f), length(f)) % EVEN 'length(s)' (Asymmetric)
% Fourier transform of the piecewise function (f)
% % ft=fft(fftshift(f));
ft = fftshift(fft(f));
% Size_ft = size(ft)
% Figure 2 shows the absolute of the fourier transform with respect to the
% frequency
figure(2);
plot(freq,abs(ft));
xlim([-100 100]) % Using 'xlim'
grid
grid('minor')
Lv = freq >= -100 & freq <= 100; % Logical Index Vector
figure(3);
plot(freq(Lv),abs(ft(Lv))); % Using 'Logical Indexing'
grid
grid('minor')
Both approaches work. What you do depends on what you want.
.
Más respuestas (0)
Ver también
Categorías
Más información sobre Fourier Analysis and Filtering 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!




