ranib
Posts: 5
Joined: Sun Nov 03, 2019 8:18 am

Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 8:22 am

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

User avatar
joan
Posts: 14986
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 10:25 am

Could you explain what you mean in more detail?

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

ranib
Posts: 5
Joined: Sun Nov 03, 2019 8:18 am

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 11:12 am

I am trying to drive a transducer maybe through the GPIO pins on the raspberry.

User avatar
joan
Posts: 14986
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 11:40 am

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?

ranib
Posts: 5
Joined: Sun Nov 03, 2019 8:18 am

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 11:53 am

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

User avatar
joan
Posts: 14986
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Generate 100Khz amplitude modulated signal

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.

ranib
Posts: 5
Joined: Sun Nov 03, 2019 8:18 am

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 12:05 pm

maybe through PWM?

User avatar
joan
Posts: 14986
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 12:08 pm

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.

User avatar
davidcoton
Posts: 5131
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 12:30 pm

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.
Location: 345th cell on the right of the 210th row of L2 cache

gordon77
Posts: 5119
Joined: Sun Aug 05, 2012 3:12 pm

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 12:56 pm

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)
Last edited by gordon77 on Sun Nov 03, 2019 1:28 pm, edited 1 time in total.

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 1:20 pm

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

drgeoff
Posts: 10924
Joined: Wed Jan 25, 2012 6:39 pm

Re: Generate 100Khz amplitude modulated signal

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.
Quis custodiet ipsos custodes?

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 3:00 pm

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

gordon77
Posts: 5119
Joined: Sun Aug 05, 2012 3:12 pm

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 3:05 pm

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.
Attachments
plot2.jpg
plot2.jpg (17.13 KiB) Viewed 2438 times
plot.jpg
plot.jpg (21.23 KiB) Viewed 2465 times
Last edited by gordon77 on Sun Nov 03, 2019 5:30 pm, edited 3 times in total.

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: Generate 100Khz amplitude modulated signal

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

gordon77
Posts: 5119
Joined: Sun Aug 05, 2012 3:12 pm

Re: Generate 100Khz amplitude modulated signal

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 ?

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

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: Generate 100Khz amplitude modulated signal

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

gordon77
Posts: 5119
Joined: Sun Aug 05, 2012 3:12 pm

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 5:22 pm

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 ?

User avatar
davidcoton
Posts: 5131
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 5:47 pm

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.
Location: 345th cell on the right of the 210th row of L2 cache

cmrincon
Posts: 44
Joined: Thu May 24, 2018 7:39 pm

Re: Generate 100Khz amplitude modulated signal

Sun Nov 03, 2019 10:26 pm

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.

User avatar
Gavinmc42
Posts: 4547
Joined: Wed Aug 28, 2013 3:31 am

Re: Generate 100Khz amplitude modulated signal

Mon Nov 04, 2019 1:45 am

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.

.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

drgeoff
Posts: 10924
Joined: Wed Jan 25, 2012 6:39 pm

Re: Generate 100Khz amplitude modulated signal

Mon Nov 04, 2019 8:15 am

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.
Quis custodiet ipsos custodes?

cmrincon
Posts: 44
Joined: Thu May 24, 2018 7:39 pm

Re: Generate 100Khz amplitude modulated signal

Mon Nov 04, 2019 8:57 am

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.

User avatar
Burngate
Posts: 6328
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Generate 100Khz amplitude modulated signal

Mon Nov 04, 2019 6:41 pm

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

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: Generate 100Khz amplitude modulated signal

Mon Nov 04, 2019 6:58 pm

You don't need to go to the complexity of a double balanced mixer to produce AM.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “Beginners”