How to find the sampling frequency?

61 views (last 30 days)
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));

Accepted Answer

Star Strider
Star Strider on 24 Oct 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
freq = 1×6002
-500.0000 -499.8334 -499.6667 -499.5001 -499.3334 -499.1668 -499.0002 -498.8335 -498.6669 -498.5002 -498.3336 -498.1670 -498.0003 -497.8337 -497.6671 -497.5004 -497.3338 -497.1671 -497.0005 -496.8339 -496.6672 -496.5006 -496.3339 -496.1673 -496.0007 -495.8340 -495.6674 -495.5007 -495.3341 -495.1675
Fv = linspace(-ts/2, ts/2-ts/length(f), length(f)) % EVEN 'length(s)' (Asymmetric)
Fv = 1×6002
-500.0000 -499.8334 -499.6668 -499.5002 -499.3336 -499.1669 -499.0003 -498.8337 -498.6671 -498.5005 -498.3339 -498.1673 -498.0007 -497.8341 -497.6674 -497.5008 -497.3342 -497.1676 -497.0010 -496.8344 -496.6678 -496.5012 -496.3346 -496.1679 -496.0013 -495.8347 -495.6681 -495.5015 -495.3349 -495.1683
% Fourier transform of the piecewise function (f)
% % ft=fft(fftshift(f));
ft = fftshift(fft(f));
Size_ft = size(ft)
Size_ft = 1×2
1 6002
% 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 Comments
Arcadius
Arcadius on 24 Oct 2023
Is there a difference between the sampling frequency chosen for the time domain and the FFT? If I wanted to compute the freq variable between -100 and 100 how can I go about that?
Star Strider
Star Strider on 24 Oct 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.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!