Page 1 of 2

Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 8:22 am
by ranib
Hi,

I want to use a RP4 to generate a 100Khz sine wave which is modulated by another 5Hz sine wave.

Is this possible? and how should I get started in programming this ( I am most comfortable with python.)

Thank you

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 10:25 am
by joan
Could you explain what you mean in more detail?

The Pi only has digital outputs (0 or 1).

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 11:12 am
by ranib
I am trying to drive a transducer maybe through the GPIO pins on the raspberry.

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 11:40 am
by joan
Okay.

Transducer: a device that converts variations in a physical quantity, such as pressure or brightness, into an electrical signal, or vice versa.

That doesn't really help. What do you want to happen at the GPIO. Yes, yhey can be switched on and off at 100 kHZ. How does that help you to achieve your goal?

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 11:53 am
by ranib
Thank you joan for your reply.

I want to be able to program a signal which is composed of a carrier frequency between 40-100 kHz which is amplitude modulated at 5-40hz. the signal should be generated by the RP and then it is supplied to an amplifier that drives the transducer. what I would like to know is whether this is possible? and if so, what is the road to take. I still have zero experience with the GPIO - I saw that there is a python package to interface with them - but I don't know in what form to program the signal

thank you

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 11:57 am
by joan
I do not understand how you can amplitude modulate a digital signal (which by definition is on or off). You can't switch a Pi GPIO 1/4 on, or 1/3 on.

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 12:05 pm
by ranib
maybe through PWM?

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 12:08 pm
by joan
I think you need to post a link to the specs of the transducer you want to drive. At the moment I feel it's a bit like the blind leading the blind.

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 12:30 pm
by davidcoton
PWM will generate both the frequencies you need -- but as square waves, not sine waves.
I think functions available in Joan's library will modulate one square wave with another square wave, maybe only at related frequencies.
BUT the Pi by itself will NOT generate sine waves, or modulate one sine wave with another.

You will need a suitably specified DAC to generate the analogue signal, probably connected via SPI for adequate speed. I don't know whether the Pi can drive the DAC fast enough to produce a smooth (modulated) sine wave, you would need to experiment. It may need calculation in a compiled language (C or C++) and an isolated CPU core, or even a real time kernel.

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 12:56 pm
by gordon77
In theory I think you can produce am by bandpass filtering the modulated pwm.

I think this will produce a carrier of 10kHz modulated with a sine wave, about 5Hz ? Note needs filtering post the Pi to give a sine wave carrier..

I have no oscilloscope or filter to prove it works !!

I don't know if a pi can do 100kHz.

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
import math

pin = 32				
GPIO.setwarnings(False)			
GPIO.setmode(GPIO.BOARD)		
GPIO.setup(pin,GPIO.OUT)
pi_pwm = GPIO.PWM(pin,10000)		
pi_pwm.start(0)				
while True:
    for deg in range(0,360,1):
        am_signal = 10 + (1 + math.sin(deg/6.29)) * 40
        pi_pwm.ChangeDutyCycle(am_signal)
        sleep(0.003)

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 1:20 pm
by PeterO
joan wrote:
Sun Nov 03, 2019 11:57 am
I do not understand how you can amplitude modulate a digital signal (which by definition is on or off). You can't switch a Pi GPIO 1/4 on, or 1/3 on.
OP's first post clearly says "generate a 100Khz sine wave" so why are you banging on about digital signals ?

PeterO

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 2:34 pm
by drgeoff
An RPi can possibly generate a 100 kHz square wave. External analogue low or band pass filtering can attenuate harmonics to approach a sine wave. The same RPi can possibly switch the 100 kHz square wave on and off at 5 Hz. The output from the filter would approximate a 100 kHz sine wave switched on and off.

In theory, a very narrow passband filter could reject everything except 999.995 kHz,100 kHz and 100.005 kHz leaving a 100 kHz sine wave 100% amplitude modulated by a 5 Hz sine wave.

In practice such a filter would be near impossible to achieve more economically than analogue modulation of a 100 kHz sine wave by a 5 Hz sine wave or by digital calculation feeding a DAC.

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 3:00 pm
by PeterO
drgeoff wrote:
Sun Nov 03, 2019 2:34 pm
An RPi can possibly generate a 100 kHz square wave. External analogue low or band pass filtering can attenuate harmonics to approach a sine wave. The same RPi can possibly switch the 100 kHz square wave on and off at 5 Hz. The output from the filter would approximate a 100 kHz sine wave switched on and off.

In theory, a very narrow passband filter could reject everything except 999.995 kHz,100 kHz and 100.005 kHz leaving a 100 kHz sine wave 100% amplitude modulated by a 5 Hz sine wave.

In practice such a filter would be near impossible to achieve more economically than analogue modulation of a 100 kHz sine wave by a 5 Hz sine wave or by digital calculation feeding a DAC.
I would do it like this ...
1) Generate 100kHz square wave and filter to sine wave (relatively easy to do)
2) Generate 5Hz sine wave with a DAC. (Could be just a few resistors on some GPIO pins and a buffer amplifier).
3) Use an analogue mixer (multiplier) to perform the modulation.
4) Buffer and filter the mixer output.

PeterO

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 3:05 pm
by gordon77
To test out the theory of am from pwm..

This generates a file which simulates pwm, with a modulation of 1/40th of the carrier ie equivalent of 10kHz and 250Hz.

Code: Select all

import math
import os

if os.path.exists('wave.txt'):
    os.remove('wave.txt')
x = 0
str = ""
while x < 100:
    x +=1
    for deg in range(0,361,1):
        am_signal = int(50 + (math.sin(deg/6.28318) * 25))
        for out in range (0,int(am_signal)):
           str += "100" + "\n"
        for out in range (int(am_signal) + 1,101):
           str +="0" + "\n"
with open("wave.txt", 'a') as f:
    f.write(str)
This then does a FFT on it..

Code: Select all

import numpy as np
import matplotlib.pyplot as plt

t = []
N = 0
with open("wave.txt","r") as textobj:
    line = textobj.readline()
    while line:
        t.append(int(line.strip()))
        line = textobj.readline()
        N +=1
t = np.array(t ) 
m = 4
nu = float(m)/N
ft = np.fft.fft(t)
freq = np.fft.fftfreq(N)/ 0.000001
plt.plot(freq, ft.real**2 + ft.imag**2)
plt.show()
and then this picture is the result showing 250Hz subcarriers on the 10kHz signal. So would need to bandpass the 10kHz and its subcarriers.

This doesn't prove the Pi can do what the OP wants.

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 5:02 pm
by PeterO
Without the phase results from the FFT it's impossible to tell if those sidebands will produce the wanted amplitude modulation, phase modulation, or some mix of the two.

PeterO

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 5:15 pm
by gordon77
PeterO wrote:
Sun Nov 03, 2019 5:02 pm
Without the phase results from the FFT it's impossible to tell if those sidebands will produce the wanted amplitude modulation, phase modulation, or some mix of the two.

PeterO
How do I show the phase results ?

It works in practice https://www.radioworld.com/tech-and-gea ... ansmitters

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 5:20 pm
by PeterO
gordon77 wrote:
Sun Nov 03, 2019 5:15 pm
PeterO wrote:
Sun Nov 03, 2019 5:02 pm
Without the phase results from the FFT it's impossible to tell if those sidebands will produce the wanted amplitude modulation, phase modulation, or some mix of the two.

PeterO
How do I show the phase results ?
I have to say that if you don't know how to do that then you probably don't understand what the FFT is telling you !
You had real and imag parts of the transform so you need to convert them into phase angles as well as the amplitude values.
PeterO

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 5:22 pm
by gordon77
PeterO wrote:
Sun Nov 03, 2019 5:20 pm
gordon77 wrote:
Sun Nov 03, 2019 5:15 pm
PeterO wrote:
Sun Nov 03, 2019 5:02 pm
Without the phase results from the FFT it's impossible to tell if those sidebands will produce the wanted amplitude modulation, phase modulation, or some mix of the two.

PeterO
How do I show the phase results ?
I have to say that if you don't know how to do that then you probably don't understand what the FFT is telling you !
You had real and imag parts of the transform so you need to convert them into phase angles as well as the amplitude values.
PeterO
OK, can you tell me ?

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 5:47 pm
by davidcoton
PeterO wrote:
Sun Nov 03, 2019 3:00 pm
I would do it like this ...
1) Generate 100kHz square wave and filter to sine wave (relatively easy to do)
...
Ah, but did you notice the additional requirement sneaked in about the fifth post?
ranib wrote: a carrier frequency between 40-100 kHz which is amplitude modulated at 5-40hz.
You'd need a very sharp low pass filter at 110kHz to get a sine wave across the whole frequency range (assuming the original square wave does indeed contain a fundamental and its odd harmonics only).
And we need a spec for the sine wave purity to assess the filter design and how to generate the modulating signal.

Re: Generate 100Khz amplitude modulated signal

Posted: Sun Nov 03, 2019 10:26 pm
by cmrincon
PeterO wrote:
Sun Nov 03, 2019 3:00 pm

I would do it like this ...
1) Generate 100kHz square wave and filter to sine wave (relatively easy to do)
2) Generate 5Hz sine wave with a DAC. (Could be just a few resistors on some GPIO pins and a buffer amplifier).
3) Use an analogue mixer (multiplier) to perform the modulation.
4) Buffer and filter the mixer output.

PeterO
Why not generate the modulation with the rp? You can use the sin function to generate a carrier and then you only have to multiply this samples with the samples of the 5 Hz sine wave. Then you only have to feed the DAC to get the output.

All have to be done in c/c++, no way to do this in python.

Cesar.

Re: Generate 100Khz amplitude modulated signal

Posted: Mon Nov 04, 2019 1:45 am
by Gavinmc42
GPCLk0 can easily generate 100KHz square wave.
It would need an LC filter to make it a sin wave.
That's how those wireless chargers work, just a different frequency.

Amplitude modulating at 5Hz that can be done a number of ways.
The GPCLK's won't go that low but a PWM can and a RC filter should work.
That how the audio outputs work.

The only tricky part is the analog mixing , which has been around before computers ;)
It could be as simple as two extra resistors.
This is basically an AM radio transmitter.

I suppose a Pi could be used as an AM transmitter, it has been used as FM.
Need to watch the transmit power and radiated RF. levels.

.

Re: Generate 100Khz amplitude modulated signal

Posted: Mon Nov 04, 2019 8:15 am
by drgeoff
Gavinmc42 wrote:
Mon Nov 04, 2019 1:45 am
The only tricky part is the analog mixing , which has been around before computers ;)
It could be as simple as two extra resistors.
No it could not. Summing two signals is completely different from modulation. It is not possible to perform amplitude modulation with only linear passive components.

Re: Generate 100Khz amplitude modulated signal

Posted: Mon Nov 04, 2019 8:57 am
by cmrincon
Gavinmc42 wrote:
Mon Nov 04, 2019 1:45 am

The only tricky part is the analog mixing , which has been around before computers ;)
It could be as simple as two extra resistors.
This is basically an AM radio transmitter.
.
You need a Guilbert Cell https://www.electronics-notes.com/artic ... -mixer.php not just 2 resistors.

Re: Generate 100Khz amplitude modulated signal

Posted: Mon Nov 04, 2019 6:41 pm
by Burngate
ranib wrote:
Sun Nov 03, 2019 8:22 am
I want to use a RP4 to generate a 100Khz sine wave which is modulated by another 5Hz sine wave.
My first reaction to the first post was to ask "how accurate do the sine-waves need to be in terms of both frequency and waveform?"
ranib wrote:
Sun Nov 03, 2019 11:53 am
... a signal which is composed of a carrier frequency between 40-100 kHz which is amplitude modulated at 5-40hz.
Now, both frequencies are variable, but we still don't know if the sine-waves need to be 16-bit accurate in shape.

The 5-40Hz sine-wave could easily be produced by filtering a PWM signal, and if the carrier doesn't actually need to be a sine-wave, chopping it with a square-wave at the carrier frequency could be made to work.
Depending on what sort of transducer it's feeding, naturally. Which we're not being told!
cmrincon wrote:
Mon Nov 04, 2019 8:57 am
You need a Guilbert Cell https://www.electronics-notes.com/artic ... -mixer.php not just 2 resistors.
MCP1496

Re: Generate 100Khz amplitude modulated signal

Posted: Mon Nov 04, 2019 6:58 pm
by PeterO
You don't need to go to the complexity of a double balanced mixer to produce AM.
PeterO