Generating two signals and concatenating them

I've got this code that is supposed to concatenate 2 singals, the problem is that i dont know what time i should put for the program to look how it is supposed to look.
ts=12000;
t=0:1/12000:159/12000;
A1=0.4;
A2=0.3;
f1=100;
f2=330;
x1=A1*sin(2*pi*f1*t);
x2=A2*sin(2*pi*f2*t);
s=[x1,x2,x1,x1,x2,x2,x1,x2];
plot(s);
It is supposed to look like this
But instead it looks like this, how should i swap the code.
The exercise sounds like: Generate a useful modulated signal corresponding to a bit string 01001101 in this way following: - for bit 0: a sinusoidal component of amplitude of 0.4V, frequency of 100Hz - for bit 1: a sinusoidal component of amplitude of 0.3V, frequency of 330Hz Each bit has a duration of 20ms without pause between bits (total signal duration is 160 ms). A sampling frequency of 12kHz is used.

 Accepted Answer

@Peter Cristian - why are you using
159/12000
? Wouldn't that correspond to
159/12000 = 0.1325 seconds
when you should have 0.02 seconds instead which corresponds to the 20 ms duration for each bit. I think you might have been confusing the 160 ms duration for the total signal with the 20 ms required for each bit (though I think using 159 in this manner is still incorrect).

7 Comments

changed t=0:1/12000:0.02; but the figure still looks wrong
@Peter Cristian - which part looks wrong? The labels along the x-axis? If so, then note that you are not providing any time data in your call to
plot(s);
Given that the duration is 160 ms, then you could use linspace to create the approriate time array as
sampleTimeMs = linspace(0, 160, length(s));
plot(sampleTimeMs, s);
There are some differences, so this is how it should look like
And this is how it looks for me, around the 20,100 ms and in the end the differences are visible.
@Peter Cristian - I suspect what is happening is that both signals (for the 0 bit and 1 bit) have been generated for the 160 ms, and then samples extracted in blocks (corresponding to the bit) from one signal to the other. This is different from what you have done where you generate a signal for each bit and then concatenate them together. For example, try
figure;
numberOfBlocks = 8;
blockSize = 240;
sampleTimeMs = linspace(0, 160, blockSize * numberOfBlocks);
x0 = A1*sin(2*pi*f1*sampleTimeMs/1000);
x1 = A2*sin(2*pi*f2*sampleTimeMs/1000);
% 0 bit signal for 160 ms
subplot(3,1,1);
plot(sampleTimeMs, x0);
% 1 bit signal for 160 ms
subplot(3,1,2);
plot(sampleTimeMs, x1);
% merged signal according to 01001101 for 160 ms
subplot(3,1,3);
x3 = [x0(1:blockSize) x1(blockSize+1:2*blockSize) x0(2*blockSize+1:4*blockSize) x1(4*blockSize+1:6*blockSize) x0(6*blockSize+1:7*blockSize) x1(7*blockSize+1:end)];
plot(sampleTimeMs, x3);
and the third figure shows something similar to the "expected" results. It isn't clear to me if what you've done is incorrect or is just a different way to get something similar.
Ok, this does the best job, but i have 2 questions, why is blockSize 240, and how exactly did you choose it, and why are x0 and x1 sampleTimeMs/1000?
The samling rate is 12KHz, so 12000 samples per second. Since you are only interested in 160 ms, then this means that there are
12000 * 0.160 = 1920
samples for the 160 ms period. Since there are 8 blocks (because of the 8 bits) then
1920 / 8 = 240
samples per block. Note that sampleTimeMs is an array of integers from 0 to 160. This is divided by 1000 to convert from milliseconds to seconds.
Ok, i got it now, thanks for your explanations, you have helped me a lot.

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Tags

Community Treasure Hunt

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

Start Hunting!