how to plot this waveform - matlab

t = 0:.001:.2;
f=10;
x=sin(2*pi*f*t);
n=15;
y=sin(2*pi*n*t);
x(x<=0) = 0;
plot(t,x);
hold on
plot(t,y);
this is the waveform that i need
togenerate:
and this is the waveform i have so far:
the first part its a simple sin but the second part when the sin doesn't go
into the negative part I am struggling with when I set it the negative
interval to 0 its going to be 0 for the whole interval not just for a point

The supplied image has two sine waves plotted next to each other, of which the one has a negative sign.
How about this:
t = -4:0.1:4;
y = 0.5.*sign(t).*sin(t*4)
plot(t,y)

Related

What do the x axis really represent in this fourier transform and how to convert it?

This is a question for a hand in in one of my courses, just want to state that.
What I am trying to do is sampling a square wave, take the fourier transform (fft) and plot the answer to graph. This is how I have achieved this:
Fs = 100;
Ts = 1/Fs;
N = 8192;
Tmax = (N - 1)*Ts;
t = 0:Ts:Tmax;
x = square(t);
X = fft(x,N);
plot(t, abs(X))
What it return is a graph that looks like this
This looks almost as inspected, but since I do not know what to expect with the square wave I also try to do it with a $\sin(2*t)$ wave. If I take the fourier transform on this, I should get 2 spiks, each at 2 and -2 (right side). But what I get is something like this
(Note! I have zoomed in on the left hand side of the graph to show that the spike is not at 2) As you can see the spike is not where it is supposed to be. I can than conclude that probably the 1 graph is not eater how it should be.
Is it something wrong with my x axis representation? And if so, how do I convert the x axis into the frequency plane?
The frequencies resulting from the FFT range from 0 to the sampling frequency. Specifically, the horizontal axis of the FFT corresponds to frequencies 0, fs/N, 2*fs/N, ... ,(N-1)*fs/N, where fs is the sample frequency and N is the FFT size.
So, you should modify the horizontal axis in the plot to the following, where N is numel(t) and fs is computed as 1/(t(2)-t(1)):
freq_axis = (0:numel(t)-1)/numel(t)/(t(2)-t(1));
plot(freq_axis, abs(X))
You may also want to apply fftshift to observe frequencies from -fs/2 to fs/2, instead of from 0 to fs. In that case:
freq_axis = (-numel(t)/2:numel(t)/2-1)/numel(t)/(t(2)-t(1));
plot(freq_axis, fftshift(abs(X)))
As a check, with your example x = sin(2*t) the second plot gives:
Comparing your sin(2*t) with the generic expression sin(2*pi*f*t), the frequency f of that sinusoid is seen to be 1/pi = 0.3183, in agreement with the figure.

Wave interference: adding two waves with opposite phases + using FFT - MATLAB problems

I have two main questions, but starting from the beginning:
I wanted to know how FFT (fast Fourier transform) works on real examples. I created two sinusoidal waves (for example Wave1 = 5 Hz and Wave2 = 15 Hz amplitude), then I added those two and made FFT from third "Wave3". It looks OK - I saw my "peaks" around 5 and 15 Hz.
Blue = 5 Hz, Red = 15 Hz, Yellow = Blue + Red. FFT from "Yellow" Wave, looks good:
OK, and then I have changed the data. Now I have two waves with identical amplitudes but opposite phases. If I add them, their amplitude is 0 - and that seems correct to me.
Two waves with opposite phases. Yellow - Wave1+Wave2 + Strange FFT of the yellow wave:
And now is the part that I don't understand at all. Here are my questions:
1) Even if I see on the picture that this third Yellow wave has an amplitude equal to 0, it's not like that in data tables. After adding two main waves (and they have opposite data!) I get the strange result.
Example: 5 first points in the data
Wave 1:
0,0627905195293128
0,125333233564304
0,187381314585724
0,248689887164855
0,309016994374947
Wave 2:
-0,0627905195293134
-0,125333233564304
-0,187381314585724
-0,248689887164855
-0,309016994374947
Wave 3 (Sum) :
-5,68989300120393e-16
-1,11022302462516e-16
-1,11022302462516e-16
3,05311331771918e-16
-1,11022302462516e-16
Why the sum of these waves is not equal 0, as it is shown in the picture? Why FFT looks so strange? Is there even a possibility that FFT will show us the real amplitudes of two identical waves with opposite phases? I thought it will not, but what's the truth?
Here is my MATLAB code:
THREE WAVES:
D = 1; % 1 second
S = 1000; % sampling rate
P = 0.5; % phase
T = 1/S; % sampling period
t = [T:T:D]; % time
myphi=2*pi*P;
myphi2=2*pi*1;
syn3 = sin(2*10*t*pi+myphi); % first wave
syn2 = sin(2*10*t*pi+myphi2); % second wave
sinmax=syn2+syn3; % yellow wave
figure; plot(t,syn3,t,syn2,t,sinmax,'LineWidth',2); grid on;
xlabel('Time (seconds)');
ylabel('Amplitude');
FFT CODE:
L = length(sinmax);
myFFT = fft(sinmax,S);
myFFT=myFFT/L; %scale the output to 1
freq = S/2*linspace(0,1,S/2);
figure; stem(freq,abs(myFFT(1:length(freq))));
xlabel('Frequency (Hz)');
ylabel('Amplitude');
Thank you very much in advance...
Mary
First things first, your calculations are correct.
Because the two graphs are auto-resized, it is easy to make a mistake interpreting them but the amplitudes are way smaller on the 2nd one than the 1st (10e-16 vs. 10e1, respectively).
On the 2nd graph, the one which leaves you puzzled, you are just victim of numerical errors : these numbers can be interpreted as 0.
From there, you have two simple solutions :
you are fine with the results you have, and you can just tweak the figures to display results on the same scale to avoid any misleading interpretation
you would like to have "proper zero values" instead of "small ones"
1) Set a limit for the Y-axis
You can just add something like this line (it is just an example - change it to meet your needs) when plotting your figures :
ymax = max(abs(my_fft));
ymin = - ymax;
ylim([ymin ymax])
2) Set a limit for filtering numerical errors
Like in a lot of numerical methods algorithms, you might want to consider as 0 values which are in between 0 and small interval, often called epsilon :
abs_fft = abs(my_fft);
epsilon = 10e-12 % your threshold
abs_fft(abs_fft < epsilon) = 0;
You might want to check out eps which is a built-in Matlab variable, meant for this kind of cases.

Generating a sine signal with time dependent frequency in Matlab

I want to generate a sine signal y(t) with a time dependent frequency f(t) in Matlab.
I've already tried to realise this using the sine function of Matlab:
h = 0.0001;
npoints = 150/h;
for i = 1:1:npoints
f(i) = 2 - 0.01*i*h;
y(i) = 0.5*sin(2*3.1415*f(i)*i*h)+0.5;
end
where the frequency is decreasing with time and h is the time step width.
My problem:
The signal y(t) doesn't look I expected it to look like. There appears a bump in the amplitude at a distinct time (have a look at the plot below).
Does anyone know why this happens and how to generate this sine signal correctly?
what about
y(i) = 0.5*real(exp(1i*2*pi*f(i)*i*h))+0.5;
You will get the plot below
If you just need a chirp signal starting from 2Hz down to 0.5Hz, the following should do the job
f_start = 2; % start frequency
f_end = 0.5; % end frequency
endtime = 150; % seconds
timestep = 0.0001;
times = timestep:timestep:endtime;
y = chirp(times,f_start,endtime,f_end);
and if you plot it you get
figure(2);plot(times,y);
You can achieve the same manually using below
f_start = 2; % start frequency
f_end = 0.5; % end frequency
timestep = 0.0001;
T = 150;
rate_of_change = (f_start - f_end)/T;
times = timestep:timestep:T;
y = sin(2*pi*(f_start*times - times.^2*rate_of_change/2));
It might be useful to read the following Wikipedia page on Chirp signal.
At 100 you have sin(2*pi*N), which is 0. Change f a little bit, say to 2.0123-... and it goes to the top.
As for the general probably unexpected shape, consider what function you are using in the end (= substitute f back in the formula). You see that you have something of the form y = ...sin(Ai-B*i^2)..., which has a minimum at 100.
The easiest solution here is to simply offset frequency a little more, and use something like f(i) = 3.1 - ..., which has a minimum outside of your considered range.
It looks like there isn't actually a "bump in frequency", but at the 100 value on the x-axis the entire signal is shifted by 180 degrees. Bear in mind that the amplitude still reaches 0 and does not become smaller (e.g. from 0.25 to 0.75)
This is because the i value becomes so high that the value of f(i) changes sign.
Another indicator of this is that the frequency starts to increase again after the shift instead of gradually becoming even lower.
Why do you start off with the value of 2 in f(i)?
Sorry for asking for clarification here, but I cannot post it as a comment.

How to calculate / create a phase shift for a signal created by joining arrays

I have created a signal in octave / matlab that is half a sin wave, (variable sin_half) and half a square wave, (variable square_half) and I would like to phase shift the combined signals, (variable comb_sig) as it's one joined signal. How can I do this?
My goal is to make a periodic signal out of joining the 1st half of one signal and the 2nd half of another signal and appending them together. Then I want to phase shift that combined signal as if it was one signal.
As you can see from the animation in (figure 1) what I currently have is the sinwave half of the signal is only phase shifting from the "center" and the square wave half is only phase shifting to the "center" instead of phase shifting the whole signal together, like the example in (figure 2). How can I phase shift the variable comb_sig all together as one combined signal like in (figure 2)?
Heres a static image of what 0 degree and 180 degree phase shift should look like. In my animation on figure 1 the square wave never makes it over to the other side when the phase shift is 180 degrees how can I fix this?
See code below:
Vth = 0; % Threshold
amp = 1; % Amplitude of input/output
freq = 1; % frequency
for n=1:2:360 %phase shift
ysin = amp*sin(freq*t+(n*pi/180)); % Entire sine wave creation
ysquare = amp*sign(ysin - Vth); % Entire square wave creation
sin_half=ysin(1,1:length(ysin)/2); % 1st half of wave choosen (sine wave)
square_half=ysquare(end-length(ysquare)/2+1:end); % 2nd half of wave choosen a (square wave)
comb_sig=[sin_half,square_half]; % 1st half and 2nd half of waves combined together
plot(t,comb_sig)
axis([-.1 2.2*pi -1.5 1.5])
pause(.01)
end
Does anyone know if there is a formula / equation / the steps required that will allow me to do this. I was thinking that the variable comb_sig needs to be multiplied by something but I'm not sure by what.
PS: I'm using octave 4.0 which is similar to matlab
Here is the updated matlab code.
amp = 1; % Amplitude of input/output
freq = 1; % frequency
time = 1/freq; %time
t=0:0.001:(time-0.001);
tHalf=t(1:size(t,2)/2);
ysinHalf = amp*sin(2*pi*freq*(tHalf)); % First half sine wave creation
ysquareHalf = -amp*ones(1,size(ysinHalf,2)); % Last half square wave creation
ywave=[ysinHalf ysquareHalf];
for n=0:2:360
phaseShifted=circshift(ywave,[0 -round(n*size(ywave,2)/360)]);
plot(t,phaseShifted);
axis([0 time -(amp+0.5) (amp+0.5)]);
pause(.01)
end

multiplying sin waves / cos waves recursively in a loop in matlab / octave

I'm trying to multiply sin waves / cosine waves recursively but I'm not sure why my answers are so different. Y2 in blue is what I'm trying to get but the FOR loop which is Y in red is what is produced any idea how to fix the FOR loop Y? see plot and code below ?
Fs = 8000;% Sampling frequency
t=linspace(0,1,Fs);
y=zeros(1,length(t));
y = .5*sin(2*pi*2*t);
for ii=1:1:3
y=y.*y;
end
plot(y,'r')
hold on
y2=(.5*sin(2*pi*2*t)).* (.5*sin(2*pi*2*t)).*(.5*sin(2*pi*2*t)); %should look like this
plot(y2,'b') %both plots should look like this
PS: I'm using octave 3.8.1 which is like matlab
You're multiplying eight times here
y = .5*sin(2*pi*2*t);
for ii=1:1:3
y=y.*y;
ii=1:1:3 is inclusive, so you do y=y.*y three times.
First time it becomes y = y^2,
Second time it become y^4 = y^2*y*2
Third time it becomes y^8 = y^4*y^4
This would be a solution:
Fs = 8000;% Sampling frequency
t=linspace(0,1,Fs);
y=zeros(1,length(t));
y = .5*sin(2*pi*2*t);
result = ones(1,length(t));
for ii=1:1:3
result=result.*y;
end
plot(result,’r’)
hold on
y2=(.5*sin(2*pi*2*t)).* (.5*sin(2*pi*2*t)).*(.5*sin(2*pi*2*t)); %should look like this
plot(y2,'b') %both plots should look like this
In all iteration, y.*y make square of updated y.
Fs = 8000;% Sampling frequency
t=linspace(0,1,Fs);
y=ones(1,length(t));
x = .5*sin(2*pi*2*t);
for ii=1:1:3
y=y.*x;
end
plot(y,'r')
hold on
y2=(.5*sin(2*pi*2*t)).* (.5*sin(2*pi*2*t)).*(.5*sin(2*pi*2*t)); %should look like this
plot(y2,'b') %both plots should look like this
Maybe, you want this code.