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)
OP's first post clearly says "generate a 100Khz sine wave" so why are you banging on about digital signals ?
I would do it like this ...drgeoff wrote: ↑Sun Nov 03, 2019 2:34 pmAn 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.
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)
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()
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 !
Ah, but did you notice the additional requirement sneaked in about the fifth post?
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).ranib wrote: a carrier frequency between 40-100 kHz which is amplitude modulated at 5-40hz.
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.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.
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.
You need a Guilbert Cell https://www.electronics-notes.com/artic ... -mixer.php not just 2 resistors.
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?"
Now, both frequencies are variable, but we still don't know if the sine-waves need to be 16-bit accurate in shape.
MCP1496cmrincon wrote: ↑Mon Nov 04, 2019 8:57 amYou need a Guilbert Cell https://www.electronics-notes.com/artic ... -mixer.php not just 2 resistors.