How to make a nonperiodic signal periodic?

I have the below code. If you run it, you'll see a graph that runs from t = 0 to t = 7. What I'd like to do is make this signal continuous, as in it runs forever from t -∞, +∞. Of course I looked it up and I see many users use stem or syms functions to make signals but they never have signals with this many piece wise components.
Thanks so much!
clc
close all
%
t=linspace(-1,8,2356);
v_t=-2*t.*fun_unit_step_gen(t,0);
v_t= v_t+2*(t-1).*fun_unit_step_gen(t,1);
v_t= v_t+3*fun_unit_step_gen(t,2);
v_t= v_t+(t-2).*fun_unit_step_gen(t,2);
v_t= v_t-5*(t-3).*fun_unit_step_gen(t,3);
v_t= v_t+3*(t-4).*fun_unit_step_gen(t,4);
v_t= v_t+5*(t-5).*fun_unit_step_gen(t,5);
v_t= v_t-4*(t-6).*fun_unit_step_gen(t,6);
v_t= v_t-fun_unit_step_gen(t,7);
%
plot(t,v_t,'r','LineWidth',2);
axis([-1 9 -4 4])
grid
title('Function f1(t)')
hold on
px=[0,.001];py=[-5,5];
plot(px,py,'-.k','LineWidth',1)
py=[0,.001];px=[0,9];
plot(px,py,'-.k','LineWidth',1)
hold off

3 Comments

What is fun_unit_step_gen function?
Since you did not provide the function fun_unit_step_gen(), I made a guess. Is my guess correct?
t=linspace(-1,8,2356);
v_t=-2*t.*(t>=0);
v_t= v_t+2*(t-1).*(t>=1);
v_t= v_t+3*(t>=2);
v_t= v_t+(t-2).*(t>=2);
v_t= v_t-5*(t-3).*(t>=3);
v_t= v_t+3*(t-4).*(t>=4);
v_t= v_t+5*(t-5).*(t>=5);
v_t= v_t-4*(t-6).*(t>=6);
v_t= v_t-(t>=7);
%
plot(t,v_t,'r','LineWidth',2);
axis([-1 9 -4 4])
grid
title('Function f1(t)')
hold on
px=[0,.001];py=[-5,5];
plot(px,py,'-.k','LineWidth',1)
py=[0,.001];px=[0,9];
plot(px,py,'-.k','LineWidth',1)
hold off
The funciton as plotted will be discontinuous at t=0, 7, 14,..., if you repeat it at those intervals. Is that OK?
If you want dot-dash lines along the axes, you can do it more simply with xline() and yline():
plot(-1:8,-3+6*rand(1,10),'-r');
xline(0,'-.k'); yline(0,'-.k'); grid on

Sign in to comment.

 Accepted Answer

I had the same idea of @William Rose, here is a possible implementation
clear variables, close all
% anonymous function
v_t = @(t)-2*t.*(t>=0)+...
2*(t-1).*(t>=1)+...
3*(t>=2)+...
(t-2).*(t>=2)+...
-5*(t-3).*(t>=3)+...
3*(t-4).*(t>=4)+...
5*(t-5).*(t>=5)+...
-4*(t-6).*(t>=6)+...
-(t>=7);
% anonymous function with period T using mod
T = 8;
v_t_per = @(t,T)v_t(mod(t,T));
% time axis
t=linspace(-8,16,2356*3);
% plot
figure, hold on, grid on
plot(t,v_t(t),'r','LineWidth',2);
plot(t,v_t_per(t,T),'b:','LineWidth',2);

11 Comments

I used here the an anonimous function, but you can simply change in your function definition t with mod(t,T)
Thank you @Fabio Freschi and @William Rose for the advice. Sorry William for not providing the code for fun_unit_step_gen(). I had forgotten I was using a function provided to the class from my professor. Honestly both of your implementations for what I asked were very helpful. This became an issue because we had to take the fft of a periodic version of this signal. With your help I was able to do that!
Paul
Paul on 20 Sep 2023
Edited: Paul on 20 Sep 2023
Typically, fft is used on only one period of periodic signal, assuming the goal is use fft to compute Fourier Series coefficients, so maybe there isn't really a need to represent the periodic signal in its entirety?
@Paul you are probably correct, I'll fast fourier transform both the nonperiodic signal and the periodic signal to observe if there are differences in the spectrum. I figured there would be because we were taught that nonperiodic signals have continuous spectrums, whereas periodic signals have discrete spectrums.
Based on this Answer thread, it seems like v_t is being called the non-periodic function and v_t_per is being called the periodic function. How would one take the fft of v_t_per? By definition of the FFT, it is only applicable to a finite duration signal (a periodic signal has infinite duration) and it also, by definition, assumes that the FFT is being applied to one period of a periodic signal. So one could extract one period of v_t_per and apply the FFT to that, but that would be the same as applying the FFT to samples of v_t for 0 <= t < 7.
So I guess it really depends on what you're trying to demonstrate and how you're trying to do that demonstration.
Based on what I've read so far, it sounds like the goal is to compare the Continuous Time Fourier Transform (CTFT) of v_t, which a continous function of frequency to the CTFT of v_t_per, which is defined by an impulse train with each impulse scaled by a correspoding Fourier Series coefficient. Approximations to the CTFTs of v_t and v_t_per can be developed using the FFT.
@Paul I had no idea that FFT is applicable to only finite duration signals. Is ctft a potential function I could use in place of fft? Thank you for your insight and for going above and beyond to help me past the question originally asked.
Since you are asking, it is worth being clear about the different forms of the Fourier transform (FT). The discrete Fourier transform (DFT) is the FT of a sampled signal. (The FFT is an algorithm for computing the DFT efficiently, and has become synonymous with the DFT. You don't have to compute the DFT with the FFT algorithm, but you might as well.) The DFT is defined for signals of finite length - call it length N (samples). The DFT assumes that x(n) repeats with period N. To put it another way, when you compute the DFT for a signal of length N, you get the coefficients for an infinite length signal with period N.
The DFT has a finite number of coefficients, and they correspond to discrete frequencies. Those discrete frequencies are the only frequencies you need to reconstruct a periodic signal with length N. The spacing between those discrete frequencies is cycles per sample.
You say "I'll fast fourier transform both the nonperiodic signal and the periodic signal to observe if there are differences in the spectrum. I figured there would be because we were taught that nonperiodic signals have continuous spectrums, whereas periodic signals have discrete spectrums."
You were taught correctly: a non-periodic signal has a continuous spectrum. A non-periodic signal is a periodic one, in which the period N goes to infinity. As I said above, the frequency spacing of the DFT is 1/N, so the frequency spacing as . And corresponds to a continuous spectrum.
How do you plan to "fast fourier transform the nonperiodic signal"? You can't. You cannot have a vector of infinite length in Matlab. You can get closer to an infinite length signal by adding a lot of zeros on either side of your signal, the computing the DFT (with the fft() function) of the signal with th added zeros.
If your non-periodic signal is a signal y(n) defined from n=0 to N-1, and 0 at all times before and after, then the way to think about it is:
y(n)=x(n)*b(n),
where x(n) is periodic with period N, and b(n) is the boxcar function, which is 1 from n=0 to N-1, and 0 elsewhere, and where * stands for standard multiplication at each time point. Multiplication in the time domain is equivalent to convolution in the frequency domain, so the FT of y(t) is given by
Y(f)=conv(X(f),B(f))
B(f) for a boxcar is well known, and you can get X(f) via the DFT (i.e. by using fft()).
Signal y(n) and b(b) are zero for an infinite time and non-zero for a finite time, so their FTs become vanishingly small.
I thought about these issues when I was doing experiments in which I created a "white noise" signal of duration 30 s with one computer and injected it into my system, and with another computer I collected data for 30 s increments. The clocks on the computers were not synced, so my data collection might be a bit shorter or longer than the repeat period of the injected signal. I had this image of combs in the frequency domain whose teeth did not exactly line up, and it worried me. What would it do to my measured frequency response spectra? I did some analysis, and it turned out I didn't need to be worried. The analysis required thinking carefully about how the discrete Fourier transform of a sampled signal is related to the continuous Fourier transform of the underlying continuous signal.
Paul
Paul on 20 Sep 2023
Edited: Paul on 21 Sep 2023
There is also the Discrete Time Fourier Transform (DTFT) that can be applied to either finite duration or infinite duration signals (periodic or not), and the DTFT is a function of continuous frequency with a "continuous spectrum" in the sense that you're interpreting that phrase, i.e., delta-F -> 0, which is different than what I assumed the OP meant by that phrase. Further clarification needed from the OP as to what the goal of the problem actually is.
If you want to analyze continuous-time signals that can be expressed in closed form, as is the case with v_t, then fourier is a good place to start. If you want to analyze discrete-time signals, then fft and/or freqz are useful for finite duration signals, and fft is useful for periodic signals. As always one has to pick the right tool for the job and interpret the outputs correctly based on the problem statement.

Sign in to comment.

More Answers (2)

How about making it for one chunk, and then using repmat to make copies of it? You can't go from t → -∞, +∞ but you can go for some finite number of elements (indexes).
If your goal is for the signal to repeat so that x(7...14)=x(0...7), and x(-7...0)=x(0...7), then I recommend using modulo division by 7 of the time argument.

Categories

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

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!