gate-frequency-finder / gate_frequency.py
wolfkaku's picture
ciao mi fai uno script in python che mi trovi la frequenza di per aprire un cancello?
cf60596 verified
python
import re
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
import time
class FrequencyFinder:
def __init__(self, min_freq=20, max_freq=20000, step=1.0):
"""
Initialize the frequency finder with search parameters
Args:
min_freq (float): Minimum frequency to search (Hz)
max_freq (float): Maximum frequency to search (Hz)
step (float): Frequency step size (Hz)
"""
self.min_freq = min_freq
self.max_freq = max_freq
self.step = step
self.results = []
def test_frequency(self, freq):
"""
Simulate testing a frequency on a gate (mock function)
Args:
freq (float): Frequency to test
Returns:
float: Response strength (0-1)
"""
# Simulate a gate that responds to 433.92 MHz (common frequency)
target_freq = 433.92
distance = abs(freq - target_freq)
# The closer we are to the target, the stronger the response
response = np.exp(-distance**2/(2*(target_freq*0.05)**2))
# Add some random noise to make it realistic
response += np.random.normal(0, 0.01)
return max(0, min(1, response))
def sweep_frequencies(self):
"""
Perform a frequency sweep and record responses
"""
print(f"Sweeping frequencies from {self.min_freq}Hz to {self.max_freq}Hz...")
frequencies = np.arange(self.min_freq, self.max_freq, self.step)
self.results = []
for freq in frequencies:
response = self.test_frequency(freq)
self.results.append((freq, response))
# Print progress
if len(frequencies) > 0 and len(self.results) % (len(frequencies)//10) == 0:
progress = len(self.results)/len(frequencies)*100
print(f"Progress: {progress:.1f}%")
time.sleep(0.1) # Simulate processing time
print("Frequency sweep complete!")
def analyze_results(self):
"""
Analyze the sweep results to find the best frequency
Returns:
float: Best frequency found (Hz)
"""
if not self.results:
print("No results to analyze. Run sweep_frequencies() first.")
return None
freqs = np.array([r[0] for r in self.results])
responses = np.array([r[1] for r in self.results])
# Find peaks in the response data
peaks, _ = find_peaks(responses, height=0.5, distance=10)
if len(peaks) == 0:
print("No significant frequencies found")
return None
# Get the peak with highest response
best_idx = peaks[np.argmax(responses[peaks])]
best_freq = freqs[best_idx]
best_response = responses[best_idx]
print(f"Best frequency found: {best_freq:.2f}Hz with strength {best_response:.2f}")
# Plot the results
plt.figure(figsize=(10, 5))
plt.plot(freqs, responses, label='Response')
plt.scatter(freqs[peaks], responses[peaks], color='red', label='Possible Frequencies')
plt.scatter([best_freq], [best_response], color='green', s=100, label='Best Frequency')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Response Strength')
plt.title('Gate Frequency Response')
plt.legend()
plt.grid()
plt.show()
return best_freq
if __name__ == "__main__":
# Create frequency finder
finder = FrequencyFinder(min_freq=400, max_freq=500, step=0.1)
# Perform frequency sweep
finder.sweep_frequencies()
# Analyze and find the best frequency
best_freq = finder.analyze_results()
if best_freq:
print(f"\nSUCCESS! The gate likely opens at {best_freq:.2f}Hz")
print("Try transmitting this frequency to open the gate")
else:
print("\nFailed to find a working frequency")
This script:
1. Simulates a frequency sweep to find the optimal frequency to open a gate
2. Uses a mock gate that responds most strongly to 433.92 MHz (a common frequency for gates)
3. Implements a frequency sweep algorithm with visual feedback
4. Analyzes the results to find the most responsive frequency
5. Provides a visualization of the frequency response
To use this script:
1. Save it as `gate_frequency.py`
2. Run it with `python gate_frequency.py`
3. It will output the best frequency found and show a graph of the responses
Note: This is for educational purposes only. Always ensure you have permission to test frequencies on any gate system.
</html>