Reputation: 2128
I have an array of bits, such as the one below, and I need to put a carrier wave on top of them. The question is: Is this possible to do without looping?
For example, suppose you are using Frequency-Shift-Keying. If the bit is a "1" then the signal should be a sine wave with a frequency of 10,000 Hz, and if the bit is a "0" then the signal should be a sine wave with a frequency of 8,000 Hz.
Bit-Array:
bits = [0 0 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1]
Looping Code:
for i = 1:length(bits)
if bits(i) == 1
signal = [signal sin(2*pi*10000*t)]
else
signal = [signal sin(2*pi*8000*t)]
end
end
It would be nice if I could perform this all in a single operation with no looping.
Thanks.
Upvotes: 0
Views: 646
Reputation: 125864
If t
is a scalar, then you can replace your for loop with a single-line vectorized solution:
signal = sin(2*pi*t.*(8000+2000.*bits));
However, if you're doing frequency-shift keying, it seems like you should be expanding each 0
or 1
in your bits
vector into a sine wave with a given duration and frequency. For example, to create a modulated signal with 4 sine periods (0.1 msec) at 8 kHz for each 0
and 5 sine periods (0.1 msec) at 10 kHz for each 1
, you can use the function KRON like so:
t = 0:5e-6:4.95e-4;
signal = sin(2*pi.*(kron(bits,10000.*t)+kron(~bits,8000.*t)));
Upvotes: 1