I have a exponentially decaying sinusoidal function which should decay in microseconds with frequency of 5 MHz.
function[t,x]=microsec(a, p, d, f)
% microsec plots an oscillatory transient voltage disturbance
% f = 5 MHz;
% a is the magnitude of oscillatory disturbance component
% p is the starting time of osillatory disturbance component in normal
% voltage
% d is the decay factor of oscillatory component
f = 5000000;
a = 1.0;
p = 0.03;
d = 55e4;
t=0:0.1e-6:0.1;
ff=50; %frequency of normal voltage
x=sin(2*pi*ff*t)+ a*(u(t-p).*(exp(-d.*(t-p)))).*sin(2*pi*f*(t));
%exponentially decaying sinusoidal...
%...transient element added to normal voltage
plot(t,x)
function y=u(t)
% unit step function needed to decide the starting time of disturbance
y=t>=0;
end
end
I expect this output:
But the resulting plot is not what I desired, it is blank up to the starting time of the disturbance:
Increasing the decay factor or oscillatory component frequency does not improve my result.
Someone told me, it is due to over-sampling. However, I didn't get help on
How to plot a graph for microseconds range i.e. a sine wave of duration 0.1 sec
With a frequency of 50Hz
And a disturbance starting at 0.03 sec
And the disturbance itself being only microseconds in duration.
The issue is the resolution of t with regard to the duration of the disturbance. It simply cannot be captured.
Change t to t=0:0.1e-8:0.1; and you will start seeing something. However, your disturbance decays so quickly compared to the underlying sine-wave that it is hard to capture. The following zoomed figure illustrates this. Please note the x-limits from 0.02999 to 0.03001:
Related
I am trying to plot the C.T. Signal Gaph (x(t)) in MATLAB for 0<=t<0.2 ms as mentioned in the below given figure. I have written the following code, but, not sure what should be the step size for time t in the graph. Any help would be highly appreciated:) Thanks!
t = 0:0.00002:0.0002;
f = 10000;
x = 4*sin(2*pi*f*t);
figure;
plot(t,x);
There seems to be a misstatement in the text. Ts is the sampling period, whereas the sampling rate is the inverse of the sampling period, Fs=1/Ts. With a Ts of 0.02 ms, that means the sampling rate is actually 50 kHz.
What's happening in the graph is that a continuous function, x(t) is being sampled by multiplying it with a pulse train, p(t). The p(t) function is composed of an infinite number of (Dirac) delta functions equally spaced by Ts seconds and goes on for infinity. The input also goes on for forever, since sin(t) is defined for all t. The concept here is to understand that by multiplying x(t) by p(t), you're getting samples of x(t) spaced out at Ts intervals.
Because both signals are infinite, you should just pick a meaningful time range for the analysis you wish to perform. In this case, it's educational, so just pick maybe a few cycles of the incoming frequency so you can see the samples. At 10kHz, the input should repeat every 0.1 ms. You should expect to see 5 samples per cycle of the sine wave (0.1 ms / (0.02 ms/sample) = 5 samples). I'd just plot it from -0.2 ms to 0.2 ms to see a few of the cycles. By adding '-o' as the plot option, you'll see 'o' markers wherever the samples were taken.
t = -0.0002:0.00002:0.0002;
f = 10000;
x = 4*sin(2*pi*f*t);
figure;
plot(t,x,'-o');
You can try changing the sampling period. For instance, try dividing it by 10.
t = -0.0002:0.000002:0.0002;
f = 10000;
x = 4*sin(2*pi*f*t);
figure;
plot(t,x,'-o');
I have a tone burst signal from 0.20 ms to 0.40 ms. From 0 to 0.20ms and from 0.40ms to 3.27ms it is zero. I did fft which shows frequency content around 25 kHz. The number of fft points is 32768 which is also the length of the time-domain signal.
I am trying to plot spectrogram in Matlab with the following code snippet
nfft = 32768;
dT = 1e-6;
fs = 1/dT;
window = hamming(nfft)
spectrogram(signal,window,[],nfft,fs)
Using this I am getting accurate frequency description but the time axis is a problem.
Zoomed in time domain signal:
Spectogram at 25 kHz:
You cannot get a representation that is accurate both in the time and frequency domains. This is the uncertainty principle of the Fourier transform.
What you can do is trade off time and frequency resolution by changing the window length. Compare the two spectrograms below, obtained with different window lengths. The signal (figure 1) is similar to that in your question.
The first spectrogram (figure 2) uses a long window, which gives good frequency resolution but poor time resolution. Note how the signal frequency of 10 Hz is resolved, but time information is very coarse.
Conversely, the second spectrogram (figure 3) uses a short window, which provides good time resolution but poor frequency resolution. As is seen, the signal frequency cannot be resolved, but its time location and shape are more accurate.
% Define signal
fs = 500; % sampling frequency
t = 0:1/fs:6; % time axis
fm = 10; % signal (carrier) frequency
s = cos(2*pi*fm*t).* exp(-5*(t-2).^2);
figure
plot(t,s)
% Spectrogram with long window
figure
nfft = 500;
window = hamming(nfft);
spectrogram(s,window,[],nfft,fs), view([90 -90])
% Spectrogram with short window
figure
nfft = 50;
window = hamming(nfft);
spectrogram(s,window,[],nfft,fs), view([90 -90])
I'm trying to find the maximum frequency of a periodic signal in Matlab and as i know when you convert a periodic signal to the frequency spectrum you get only delta functions however i get a few curves between the produced delta functions. Here is the code :
t=[-0.02:10^-3:0.02];
s=5.*(1+cos(2*pi*10*t)).*cos(2*pi*100*t);
figure, subplot(211), plot(t,s);
y=fft(s);
subplot(212), plot(t,y);
Here is a code-snippet to help you understand how to get the frequency-spectrum using fft in matlab.
Things to remember are:
You need to decide on a sampling frequency, which should be high enough, as per the Nyquist Criterion (You need the number of samples, at least more than twice the highest frequency or else we will have aliasing). That means, fs in this example cannot be below 2 * 110. Better to have it even higher to see a have a better appearance of the signal.
For a real signal, what you want is the power-spectrum obtained as the square of the absolute of the output of the fft() function. The imaginary part, which contains the phase should contain nothing but noise. (I didn't plot the phase here, but you can do this to check for yourself.)
Finally, we need to use fftshift to shift the signal such that we get the mirrored spectrum around the zero-frequency.
The peaks would be at the correct frequencies. Now considering only the positive frequencies, as you can see, we have the largest peak at 100Hz and two further lobs around 100Hz +- 10Hz i.e. 90Hz and 110Hz.
Apparently, 110Hz is the highest frequency, in your example.
The code:
fs = 500; % sampling frequency - Should be high enough! Remember Nyquist!
t=[-.2:1/fs:.2];
s= 5.*(1+cos(2*pi*10*t)).*cos(2*pi*100*t);
figure, subplot(311), plot(t,s);
n = length(s);
y=fft(s);
f = (0:n-1)*(fs/n); % frequency range
power = abs(y).^2/n;
subplot(312), plot(f, power);
Y = fftshift(y);
fshift = (-n/2:n/2-1)*(fs/n); % zero-centered frequency range
powershift = abs(Y).^2/n;
subplot(313), plot(fshift, powershift);
The output plots:
The first plot is the signal in the time domain
The signal in the frequency domain
The shifted fft signal
Here is my code for generating a triangular waveform in the time domain and for generating its corresponding fourier series/transform (I don't know whether its series or transform because matlab only has fourier transform function but since the signal is periodic, references say that the fourier counterpart must be called fourier series).
x = 0;
s = 50; % number of sinusoidal components
fs = 330; % hertz
dt = 1/fs; % differential time
t = [0:dt:4]; % seconds
const = 2 / (pi^2);
for k = 1:2:s,
x = x + (((-1)^((k - 1) / 2)) / (k^2)) * sin(4*pi*k*t);
end
x = const * x;
% amplitude = max(x) = 0.2477
% period = 0.5 seconds
f = linspace(-fs/2,fs/2,length(x));
xk = fftshift(fft(x));
figure;
subplot(3,1,1);
plot(t,x);
grid on;
xlabel('time(seconds)');
title('Time Domain');
subplot(3,1,2);
plot(f,abs(xk));
grid on;
xlabel('frequency(hertz)');
title('Magnitude Spectrum');
subplot(3,1,3);
plot(f,angle(xk));
grid on;
xlabel('frequency(hertz)');
title('Phase Spectrum');
And here is the generated plots for the time domain signal, magnitude spectrum, and phase spectrum.
link:
fs = 330hz
My problem is when I changed the sampling frequency (fs which is currently equal to 330 hz) to another value, the plots of the magnitude and phase spectra change.
Here is the plots of the magnitude and phase spectra when the sampling frequency is equal to 400 hz:
link:
fs = 400 hz
Can you explain why does this happen? And what can I do in order to get a constant plots for the magnitude and phase spectra given any sampling frequency?
I can't get your pictures to load over my proxy, but the spectrum of a FFT will be have a bigger "gap" in the middle at a higher sampling rate. A fundamental property of sampling is that it introduces copies of your original spectrum; you may have learned this if you studied the discrete-time Fourier transform. At a higher sampling rate, these copies are farther apart.
Additionally, your sampling points will be in different places at different sampling rates, so you may get different lobing behavior.
Incidentally, you are getting the discrete Fourier transform in Matlab -- you are giving it a finite sequence of discrete points, not a continuous, inifintely long signal.
If you want the plots to look the same, just make their x-axes match.
It because that the spectra by DFT/FFT are indeed the sampled and normalized version of the original analog spectra, therefore, with the sampling step changes, the sampling step in frequency domain also changes, thus the spectra lines that you see also change since the original spectra are not constant. Another factor may be the aliasing effects, since the analog spectra of triangular waveform are infinite in theory.
I am trying to plot a sine wave where the amplitude increases over time and the frequecy increases over time as well. I draw a normal sine wave as shown below but I couldn't change the amplitude and frequency. Any Ideas?
t = [ 0 : 1 : 40 ]; % Time Samples
f = 500; % Input Signal Frequency
fs = 8000; % Sampling Frequency
x = sin(2*pi*f/fs*t); % Generate Sine Wave
figure(1);
stem(t,x,'r'); % View the samples
figure(2);
stem(t*1/fs*1000,x,'r'); % View the samples
hold on;
plot(t*1/fs*1000,x); %
I believe what you are speaking of is amplitude modulation (AM) and frequency modulation (FM). Basically, AM refers to varying the amplitude of your sinusoidal signal and varying this uses a function that is time-dependent. FM is similar, except the frequency varies instead of the amplitude.
Given a time-varying signal A(t), AM is usually expressed as:
Minor note: The above is actually double sideband suppressed carrier (DSB-SC) modulation but if you want to achieve what you are looking for in your question, we actually need to do it this way instead. Also, the signal customarily uses cos instead of sin to ensure zero-phase shift when transmitting. However, because your original code uses sin, that's what I'll be using as well.
I'm putting this disclaimer here in case any communications theorists want to try and correct me :)
Similarly, FM is usually expressed as:
A(t) is what is known as the message or modulating signal as it is varying the amplitude or frequency of the sinusoid. The sinusoid itself is what is known as the carrier signal. The reason why AM and FM are used is due to communication theory. In analog communication systems, in order to transmit a signal from one point to another, the message needs to be frequency shifted or modulated to a higher range in the frequency spectrum in order to suit the frequency response of the channel or medium that the signal travels in.
As such, all you have to do is specify A(t) to be whichever signal you want, as long as your values of t are used in the same way as your sinusoid. As an example, let's say that you want the amplitude or frequency to increase linearly. In this case, A(t) = t. Bear in mind that you need to specify the frequency of the sinusoid f_c, the sampling period or sampling frequency of your data as well as the time frame that your signal is defined as. Let's call the sampling frequency of your data as f. Also bear in mind that this needs to be sufficiently high if you want the curve to be visualized properly. If you make this too low, what'll happen is that you will be skipping essential peaks and troughs of your signal and the graph will look poor.
Therefore, for AM your code may look something like this:
f = 24; %// Hz
f_c = 8; %// Hz
T = 1 / f; %// Sampling period from f
t = 0 : T : 5; %// Determine time values from 0 to 5 in steps of the sampling period
A = t; %// Define message
%// Define carrier signal
carrier = sin(2*pi*f_c*t);
%// Define AM signal
out = A.*carrier;
%// Plot carrier signal and modulated signal
figure;
plot(t, carrier, 'r', t, out, 'b');
grid;
The above code will plot the carrier as well as the modulated signal together.
This is what I get:
As you can see, the amplitude gets higher as the time increases. You can also see that the carrier signal is being bounded by the message signal A(t) = t. I've placed the original carrier signal in the plot as an aid. You can certainly see that the amplitude of the carrier is getting larger due to the message signal.
Similarly, if you want to do FM, most of the code is the same. The only thing that'll be different is that the message signal will be inside the carrier signal itself. Therefore:
f = 100; %// Hz
f_c = 1; %// Hz
T = 1 / f; %// Sampling period from f
t = 0 : T : 5; %// Determine time values from 0 to 5 in steps of the sampling period
A = t; %// Define message
%// Define FM signal
out = sin(2*pi*(f_c + A).*t);
%// Plot modulated signal
figure;
plot(t, out, 'b');
grid;
Bear in mind that I changed f_c and f in order for you to properly see the changes. I also did not plot the carrier signal so you don't get distracted and you can see the results more clearly.
This is what I get:
You can see that the frequency starts rather low, then starts to gradually increase itself due to the message signal A(t) = t. As time increases, so does the frequency.
You can play around with the different frequencies to get different results, but this should be enough to get you started.
Good luck!